如果没有要显示的帖子,如何抛出消息

I am displaying an array of posts by the following code. When there are no posts to display, I want to print a notification, e.g. "No posts to display". How can this be done?

<?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; ?>

have_posts() returns true when there are posts to display, and false when there aren't any (see the Documentation).

You can easily evaluate the value of this, and display your message accordingly. For example:

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

In your example you would have to modify your code as follows:

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