現在の記事の属しているタグの記事一覧を表示:WordPress
WordPressで現在の記事の属しているタグの記事一覧を表示する方法をメモ。
single.phpの記事一覧を表示したい箇所に
<ul> <?php $tags = wp_get_post_tags($post->ID); if ($tags) { $tag_ids = array(); foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; $args=array( 'tag__in' => $tag_ids, 'post__not_in' => array($post->ID), 'showposts'=>5, 'posts_per_page'=>5, 'caller_get_posts'=>1 ); $my_query = new wp_query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) { $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php } } } ?> </ul>
でOK。
WordPressのおすすめ参考書
bookfan 1号店 楽天市場店
¥3,300 (2025/04/22 17:56時点 | 楽天市場調べ)