個別ポスト記事(single.php)で記事下に同カテゴリ内の関連記事一覧をサムネイルつきで表示させるPHPコードの覚書です。
<?php $post_id = get_the_ID(); foreach((get_the_category()) as $cat) { $cat_id = $cat->cat_ID ; break ; } query_posts( array( 'cat' => $cat_id, 'showposts' => 10, 'post__not_in' => array($post_id) ) ); ?> <?php if (have_posts()) : ?> <h2>「<?php $cat = get_the_category(); $cat = $cat[0]; {echo "$cat->cat_name " ;} ?>」カテゴリの関連記事</h2> <div class="contents"> <?php while (have_posts()) : the_post(); ?> <div class="relate"> <span class="relate-i"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(50,50) ); ?></a></span> <span class="relate-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span> </div> <?php endwhile; ?> </div> <?php endif; ?> <?php wp_reset_query(); ?>
上記のPHPコードをsingle.phpの任意の場所に貼り付けます。
表示している個別ページは、関連リストには表示させないようにしています。
スタイルシートでカスタマイズしてください。
参考サイト:
シングルページに同一カテゴリーの記事一覧を表示するカスタマイズ | bl6.jp
追記:
テーブルタグで個別記事の概要も表示させる+ランダム表示させるPHPコード
<?php $post_id = get_the_ID(); foreach((get_the_category()) as $cat) { $cat_id = $cat->cat_ID ; break ; } query_posts( array( 'cat' => $cat_id, 'showposts' => 10, 'orderby' => rand, 'post__not_in' => array($post_id) ) ); ?> <?php if (have_posts()) : ?> <h2>「<?php $cat = get_the_category(); $cat = $cat[0]; {echo "$cat->cat_name " ;} ?>」カテゴリの関連記事</h2> <table class="relate-ta"> <?php while (have_posts()) : the_post(); ?> <tr> <td style="width:30%;"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(100,100) ); ?></a></td> <td style="width:70%;"> <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br /> <?php echo mb_substr(get_the_excerpt(), 0, 35); ?>... </p> </td> </tr> <?php endwhile; ?> </table> <?php endif; ?> <?php wp_reset_query(); ?>