使用WP_Query,但保持分页

时间:2012-04-11 04:12:40

标签: wordpress

我想让我的页面显示最近的2个帖子,所以我使用WP_Query并将posts_per_page设置为2,这很有效,但它会杀死分页。这是我的代码。如何更改它以显示最近的两个帖子并保持分页?

  <?php $wp_query = new WP_Query( array( 'posts_per_page' =>2 ));?>

  <?php if ( $wp_query->have_posts() ) : ?>

  <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>


<div <?php post_class() ?> id="post-<?php the_ID(); ?>">

    <div id="ind_post">

        <h2 class="post-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>


        <div id="entry">
        <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>

            <p><?php the_excerpt(); ?></p>
        </div>
        <h4 class="more-post"><a href="<?php the_permalink() ?>">continue reading…</a></h4>

    </div>

<?php endwhile; ?>



<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>

    <?php else : ?>

    <h2>No results found</h2>

<?php endif; ?>
        <?php wp_reset_query(); ?>

1 个答案:

答案 0 :(得分:0)

这应该这样做

<?php $wp_query = new WP_Query( array( 'posts_per_page' =>2, 'paged=' . get_query_var( 'page' ) ));?>