【问题标题】:How to display "no posts found" for wordpress tag related posts?如何为 wordpress 标签相关帖子显示“未找到帖子”?
【发布时间】:2013-08-19 11:11:47
【问题描述】:

我正在使用以下代码在 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())
?>

但是,当没有其他具有相同标签的帖子时,我希望它显示“未找到帖子”之类的内容。

任何想法我可以如何做到这一点?

【问题讨论】:

    标签: wordpress tags


    【解决方案1】:

    只需在 if($myquery->havePosts) 中添加 else 语句

    ...
        <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)
    else{
        echo "No Posts" //Look I am HERE 
    }
      } //if ($tags)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多