2

次のコードを使用して、single.php 内で同じタグを持つ投稿のリストを表示しています。

<?php
if ( is_single() ) {
  $tags = wp_get_post_tags($post->ID);
    if ($tags) {
    $first_tag = $tags[0]->term_id;
echo 'first_tag' .$first_tag;
$args=array(
  'tag__in' => array($first_tag),
  'post__not_in' => array($post->ID),
  'showposts'=>5
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  echo 'Related Posts';
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php     the_title_attribute(); ?>"><?php the_title(); ?></a></p>
   <?php
  endwhile;
} //if ($my_query)
  } //if ($tags)
  wp_reset_query();  // Restore global post data stomped by the_post().
} //if (is_single())
?>

ただ、他に同じタグの投稿が無い場合は「投稿が見つかりません」のような表示にしたいです。

これを達成する方法はありますか?

4

1 に答える 1