首先,don\'t use query_posts()
.
当前类别已可用于category.php
样板
要修改类别查询并指定每页要显示的帖子数,请使用pre_get_posts
钩住并设置posts_per_page
照着下面的代码设置posts_per_page
到2
. 将此代码添加到主题functions.php
文件:
add_action( \'pre_get_posts\', \'wpse_category_posts_per_page\' );
function wpse_category_posts_per_page( $query ) {
if ( is_admin() ) {
return;
}
if ( $query->is_main_query() && $query->is_category() ) {
$query->set( \'posts_per_page\', 2 );
}
}
已更新
category.php
模板文件:
<?php
/**
* The category template.
*
*/
get_header();
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
the_title();
the_excerpt();
echo \'<a href="\' . esc_url( get_permalink() ) . \'"> Read more...</a>\';
endwhile;
else :
echo \'There are no posts in this category.\';
endif;
?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link( \'« Previous Entries\' ) ?></div>
<div class="alignright"><?php next_posts_link( \'Next Entries »\',\'\' ) ?></div>
</div>
<?php
get_footer();