PHP Wordpress loop, how to remove loop and show single iteration instead?
This page shows many recent posts, instea开发者_如何学Cd I want it to show only the most recent. How would I remove this loop and replace it with a single iteration of the most recent?
    <?php while (have_posts()) : the_post(); ?>
        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
        <span class="postmetadata"><?php the_category(' / ') ?> — <?php edit_post_link('Edit', '', ' — '); ?>  <?php comments_popup_link('No comments', '1 comment', '% comments'); ?></span><br/>
            <small><span class="date"><?php the_time('d') ?></span><br /><?php the_time('M y') ?> <!-- by <?php the_author() ?> --></small>
            <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
            <div class="entry">
                <?php the_content('<em>Continue reading →</em>'); ?>
            </div>
            <div class="clearfix"></div>
        </div>
    <?php endwhile; ?>
Well if the posts are already sorted in the correct order and you just want the first one you could change it to:
<?php if (have_posts()): the_post(); ?>
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    <span class="postmetadata"><?php the_category(' / ') ?> — <?php edit_post_link('Edit', '', ' — '); ?>  <?php comments_popup_link('No comments', '1 comment', '% comments'); ?></span><br/>
        <small><span class="date"><?php the_time('d') ?></span><br /><?php the_time('M y') ?> <!-- by <?php the_author() ?> --></small>
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <div class="entry">
            <?php the_content('<em>Continue reading →</em>'); ?>
        </div>
        <div class="clearfix"></div>
    </div>
<?php endif; ?>
Modify the global $query_string variable:
<?php
global $query_string;
$query_string .= "&posts_per_page=1"; // append the post count limit
query_posts($query_string); // perform the query
?>
<?php while (have_posts()) : the_post(); ?>
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    <span class="postmetadata"><?php the_category(' / ') ?> — <?php edit_post_link('Edit', '', ' — '); ?>  <?php comments_popup_link('No comments', '1 comment', '% comments'); ?></span><br/>
        <small><span class="date"><?php the_time('d') ?></span><br /><?php the_time('M y') ?> <!-- by <?php the_author() ?> --></small>
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <div class="entry">
            <?php the_content('<em>Continue reading →</em>'); ?>
        </div>
        <div class="clearfix"></div>
    </div>
<?php endwhile; ?>
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论