我的分页页面在每一页上都显示出相同的内容。查询第一页以显示两篇文章,并正确显示。第二页显示了与第一页完全相同的两篇文章。这是我的代码:(functions.php)(这是我在web上找到的一个预制代码)。
function wp_corenavi() {
global $wp_query, $wp_rewrite;
$pages = \'\';
$max = $wp_query->max_num_pages;
if (!$current = get_query_var(\'paged\')) $current = 1;
$a[\'base\'] = str_replace(999999999, \'%#%\', get_pagenum_link(999999999));
$a[\'total\'] = $max;
$a[\'current\'] = $current;
$total = 1; //1 - display the text "Page N of N", 0 - not display
$a[\'mid_size\'] = 5; //how many links to show on the left and right of the current
$a[\'end_size\'] = 1; //how many links to show in the beginning and end
$a[\'prev_text\'] = \'« Previous\'; //text of the "Previous page" link
$a[\'next_text\'] = \'Next »\'; //text of the "Next page" link
if ($max > 1) echo \'<div class="navigation">\';
if ($total == 1 && $max > 1) $pages = \'<span class="pages">Page \' . $current . \' of \' . $max . \'</span>\'."\\r\\n";
echo $pages . paginate_links($a);
if ($max > 1) echo \'</div>\';
}
(自定义页面模板)
<?php
get_header();
if ( have_posts() ) : ?>
<div id="container">
<div id="content" role="main">
<?php query_posts( \'category_name=category&showposts=2\'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<div>
<div>
<div class="thumb">
<?php the_post_thumbnail(\'thumbnail\'); ?>
</div>
<h2 class="entry-title" style="text-align:center;"><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>
<div class="the-excerpt">
<?php echo content(15, __(\'(Read More)\')); /* twentyten_posted_on(); */ ?>
</div><!-- .entry-meta -->
</div>
</div>
<?php endwhile; // end of the loop. ?>
<br clear="all" />
<?php if (function_exists(\'wp_corenavi\')) wp_corenavi(); ?>
</div><!-- #content -->
</div><!-- #container -->
<?php endif; ?>
最合适的回答,由SO网友:Stephen Harris 整理而成
这是因为query_posts
重置查询。请参见Codex页面上的警告:
分页无法正常工作,除非在页面模板中使用query\\u posts(),并正确设置“paged”查询变量:http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html
电话:
query_posts( \'category_name=category&showposts=2\');
不告诉它要获取哪一页(因此它获取第一页)。这是一个简单的问题:
$page = get_query_var(\'paged\');
$page = (!empty($page) ? $page : 1);
query_posts( \'category_name=category&showposts=2&paged=\'.$page);
Note: 对于页面(即,当使用自定义页面模板时,您需要使用
get_query_var(\'page\')
而不是
get_query_var(\'paged\')
.
SO网友:Chip Bennett
首先,您在循环的内部修改查询,它从if ( have_posts() ) :
有条件的将此移动到查询外部:
<?php query_posts( \'category_name=category&showposts=2\'); ?>
其次,你不想使用
query_posts()
除了
modifying the Primary Loop. 由于这是一个自定义页面模板,因此主要循环是页面内容。您的类别查询是一个次要循环。所以
you should be using WP_Query()
instead of query_posts()
for this secondary loop.
第三,无论何时创建需要分页的二级循环,都需要告诉WordPress根据二级循环的查询分页,而不是根据主循环的查询分页。那个问题has been dealt with extensively 这里是WPSE;here\'s one example.
要根据您的用例修改它,请执行以下操作:
<?php
// Custom query args
$category_query_args = array(
\'category_name\' => \'category\'
\'posts_per_page\' => 2
);
// Custom query
$category_query = new WP_Query( $category_query_args );
// Reset pagination
global $wp_query;
$temp = $wp_query;
$wp_query= null;
$wp_query = $category_query;
// Output the Loop
if ( $category_query->have_posts() ) :
?>
<div id="container">
<div id="content" role="main">
while ( $category_query->have_posts() ) : $category_query->the_post();
?>
<div>
<div>
<div class="thumb">
<?php the_post_thumbnail(\'thumbnail\'); ?>
</div>
<h2 class="entry-title" style="text-align:center;"><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>
<div class="the-excerpt">
<?php echo content(15, __(\'(Read More)\')); /* twentyten_posted_on(); */ ?>
</div><!-- .entry-meta -->
</div>
</div>
<?php
endwhile;
// Custom navigation
?>
<br clear="all" />
<?php if (function_exists(\'wp_corenavi\')) wp_corenavi(); ?>
</div><!-- #content -->
</div><!-- #container -->
<?php
endif;
// Reset query
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
编辑,您应该使用
posts_per_page
而不是
showposts
, 已弃用。