[英]Wordpress - if there are no posts to display for a single category
如果某個類別沒有帖子,我會嘗試顯示一條消息
<?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; ?>
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';
}
?>
你可以做這樣的事情<?php if ( have_posts() ) { while ( have_posts() ) : the_post(); // Your loop code endwhile; } else { echo 'Sorry, no posts were found'; } ?>
<?php if ( have_posts() ) { while ( have_posts() ) : the_post(); // Your loop code endwhile; } else { echo 'Sorry, no posts were found'; } ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.