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

问题描述 投票:0回答:3

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

<?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; ?>
php wordpress categories posts
3个回答
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';
              }
    ?>

0
投票

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


0
投票

但是如果我想显示特定的帖子或页面而不是一行(“抱歉,找不到帖子”怎么办?否则,方案将保持不变。

© www.soinside.com 2019 - 2024. All rights reserved.