WP query_posts() - 结果忽略最近的帖子

时间:2011-12-05 17:29:48

标签: wordpress

我有以下代码(在开始之前和之后省略所有内容):

<?php query_posts( 'posts_per_page=4' ); ?>
<?php
if ( have_posts() )
the_post();
?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s',     'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<?php twentyten_posted_on(); ?>
<?php the_excerpt(); ?>
<p><?php printf( __( 'Posted in %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?></p> 
<p><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?></p>
<?php comments_template( '', true ); ?>
<?php endwhile; ?>

结果页面未显示最近的4篇帖子 - 它正在删除最近的帖子。这是实况页面:

http://www.metricit.com/blog/

这是另一个页面(使用标准循环存档代码),显示了最近的4个帖子。

http://www.metricit.com/category/blog/

我做错了什么?

由于

布雷特

2 个答案:

答案 0 :(得分:2)

为什么在循环之上有这个?

<?php
if ( have_posts() )
the_post();
?>

摆脱它 - 那是在跳过第一篇文章。

答案 1 :(得分:1)

我的猜测是,您应该删除对the_post()的第一个电话,尝试将您的代码更改为:

<?php query_posts( 'posts_per_page=4' ); ?> 
<?php if ( have_posts() ) : ?> 
<?php while ( have_posts() ) : the_post(); ?> 
<h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s',     'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<?php twentyten_posted_on(); ?> 
<?php the_excerpt(); ?> 
<p><?php printf( __( 'Posted in %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?></p>
<p><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ),__( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?></p> 
<?php comments_template( '', true ); ?>       
<?php endwhile; ?>
<?php endif; ?>