2013-04-09 56 views
0

我正在按以下代碼顯示帖子數組。當沒有可顯示的帖子時,我想要打印一個通知,例如「沒有帖子可顯示」。如何才能做到這一點?如果沒有要顯示的帖子,我該如何處理消息

<?php 
    while (have_posts()) : 
     the_post(); 
?> 
     <h1><?php the_title();?></h1> 
     <section class="intro"> 
     <?php the_content(); ?> 
     </section> 
<?php endwhile; // end of the loop. ?> 
     <h2>Latest Events</h2> 

<?php 
    query_posts(array('category__and' => array(8))); 
    if (have_posts()) while (have_posts()) : 
     the_post(); 
?> 

     <article class="events clearfix"> 
      <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> 
      <?php the_excerpt(); ?> 
      <div class="date"> 
       <span class="month"><?php the_time('M') ?></span> 
       <span class="day"><?php the_time('d') ?></span> 
      </div> 
     </article> 


<?php endwhile; ?> 

回答

0

have_posts()返回true當有訊息進行顯示,並false當沒有任何(見Documentation)。

您可以輕鬆評估此值,並相應地顯示您的消息。例如:

if(!have_posts()) 
{ 
    echo 'No posts to display&hellip;'; 
} 
else 
{ 
    // code to display your posts here. 
} 
1

在你的榜樣,你將不得不修改您的代碼如下:

// Display latest events 
// ... 
if (have_posts()) { 
    // ... 
} else { 
    echo '<article class="events clearfix">'; 
    echo '<p>No posts to display.</p>'; 
    echo '</article>'; 
} 
相關問題