Wordpress - 如果没有要显示单个类别的帖子

时间:2015-03-22 05:12:27

标签: php wordpress categories posts

如果某个类别没有帖子,我会尝试显示消息

<?php $recent = new WP_Query("cat=9&showposts=10");
     while($recent->have_posts()) : $recent->the_post();?>
        <?php the_title(); ?>
        <?php the_content();?>
<?php endwhile; ?>

但是当我添加'else'时,我得到了空白屏幕

<?php else: ?>
message ////
<?php endif; ?>

2 个答案:

答案 0 :(得分:1)

Use the following code to get rid of your problem
   <?php
      $recent = new WP_Query("cat=9&showposts=10");
       /*condition for having post*/   
       if ( have_posts() )
        {   
            /*Loop start */
            while($recent->have_posts()) : $recent->the_post();
                 the_title();
                 the_content();
            endwhile;
            /*Loop end*/
        }
         /*No post found to show the following message*/
        else {
                echo  'No Post found';
              }
    ?>

答案 1 :(得分:0)

你可以做这样的事     <?php if ( have_posts() ) { while ( have_posts() ) : the_post(); // Your loop code endwhile; } else { echo 'Sorry, no posts were found'; } ?>