wordpress 関連の投稿には、各カテゴリで作成された最近の 3 つの投稿のみが表示されます。各カテゴリにランダムな投稿を表示することを望んでいました。そのため、古い投稿でも表示できます。最後に作成された 3 つだけではありません。以下にコードを示します。
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts'=>3, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<div class="relatedpost">
<a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(175,98)); ?><br />
<center><h6><?php the_title(); ?></h6></center>
</a>
</div>