

ワードプレスの個別ページに関連記事を表示したい!もちろんカテゴリ別で!
ワードプレスのsingle.phpに直接記載する方法で進めます。
<!-- 関連記事 -->
<?php
$post_id = get_the_ID(); // 投稿のIDを取得
$categories = get_the_category($post_id); // 投稿のカテゴリーを取得
$cat_ids = []; // カテゴリーIDを格納するための空の配列を用意
foreach ($categories as $category) :
array_push($cat_ids, $category->term_id); // 用意した空配列にカテゴリーIDを格納
endforeach;
$args = [
'post_type' => 'post', // 投稿タイプを指定
'posts_per_page' => '3', // 表示する記事数を指定
'category__in' => $cat_ids, // カテゴリーIDを指定
'post__not_in' => [$post_id] // 現在の投稿を除外
];
$related_cats_query = new WP_Query($args);
?>
<div class="related">
<h3 class="related-ttl">関連記事</h3>
<?php if ($related_cats_query->have_posts()) : ?>
<ul>
<?php while ($related_cats_query->have_posts()) : $related_cats_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<div class="imgs"><?php the_post_thumbnail('full'); ?></div>
<p><?php the_title(); ?></p>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<p>関連記事はありません。</p>
<?php
endif;
wp_reset_postdata(); ?>
</div>
ワードプレスには関連記事を表示するためのプラグインも多数ありますが、やはり自作ですとカスタマイズもやり易いのでコチラがベストと思います。