表示している記事以外で同じカテゴリーに属している記事を、ランダムに表示
WordPressの個別記事でその記事以外で同じカテゴリーのものを数件ランダムで表示させます。
結構使うコードなので、メモ。
<?php
$currentId = $post->ID;
$cat = get_the_category();
$cat = $cat[0]->cat_ID;
$rand_posts = get_posts('numberposts=5&category='.$cat.'&orderby=RAND()&exclude='.$currentId);
if($rand_posts):
foreach($rand_posts as $post):
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; endif;?>