Home > wordpressでホームページ制作 > 関連する記事を表示する方法

関連する記事を表示する方法

プラグインを使わずに関連する記事を表示する方法。このサイトでも使用しています。

同じタグのものを関連する記事として表示します。
『’showposts’=>3』のようにshowpostsの値で表示させる記事の数を設定します。

<ul>
<?php $tags = wp_get_post_tags($post->ID);
if ($tags) {
$first_tag = $tags[0]-> term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>3,
'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>
<h4>
<a title="<?php the_title_attribute(); ?>" rel="bookmark" href="<?php the_permalink();?>"></a>
</h4>
<p><?php echo get_post_meta($post->ID,'short_desc',true); ?></p>
</li>

<?php endwhile; wp_reset_query(); ?>
        <?php } else { ?>
            <p>関連する記事は見当たりません…</p>
        <?php } } ?>
</ul>

タグ:


Home > wordpressでホームページ制作 > 関連する記事を表示する方法

関連する記事