此页面显示了许多最近的帖子,而我希望它只显示最新的帖子。如何删除此循环并将其替换为最近的一次迭代?
<?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; ?>
答案 0 :(得分:3)
如果帖子已经按照正确的顺序排序,你只想要第一个,你可以将其更改为:
<?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; ?>
答案 1 :(得分:1)
修改全局$ query_string变量:
<?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; ?>