在我的类别模板(category.php)中,我有以下代码:
<?php
// Setup custom loop -- we need to exclude featured posts from listing so they aren\'t repeated
$paged = get_query_var(\'paged\') ? absint(get_query_var( \'paged\' )) : 1;
$args = [
\'post_type\' => \'post\',
\'cat\'=> $current_category->cat_ID,
\'post_status\' => \'publish\',
\'paged\' => $paged,
\'posts_per_page\' => $posts_per_page,
\'orderby\' => \'date\',
\'order\' => \'DESC\'
];
// If we have posts to exclude -- add that argument
if (!empty($featured_posts_to_exclude)) {
$args[\'post__not_in\'] = $featured_posts_to_exclude;
}
$category_posts = new WP_Query($args);
if ( $category_posts->have_posts() ) :
// Used in template part to vary content
$loop_count = 0;
/* Start the Loop */
while ( $category_posts->have_posts() ) : $category_posts->the_post();
// We use this instead of "get_template_parts" so that we can pass loop vars
include(locate_template(\'template-parts/content-horz-ads.php\', false, false));
$loop_count++;
endwhile;
// Add pagination
im_numeric_posts_nav($category_posts);
// Reset since we are using a custom loop.
wp_reset_postdata();
else :
get_template_part( \'template-parts/content\', \'none\' );
endif;
我的分页函数如下所示(我发布它是为了完整性,但无论posts\\u per\\u page设置为10,还是6,或者它是否是自定义循环,它都工作得非常好--它显示正确的链接和链接数):
function im_numeric_posts_nav($custom_query_object = null) {
// If we\'re on a singular page, we don\'t need navigation
if (is_singular()) {
return;
}
// If a custom loop was passed in, use it...otherwise use global loop
if ($custom_query_object !== null) {
$wp_query = $custom_query_object;
} else {
global $wp_query;
}
/** Stop execution if there\'s only 1 page */
if ($wp_query->max_num_pages <= 1) {
return;
}
$paged = get_query_var(\'paged\') ? absint(get_query_var( \'paged\' )) : 1;
$max = intval($wp_query->max_num_pages);
/** Add current page to the array */
if ($paged >= 1) {
$links[] = $paged;
}
/** Add the pages around the current page to the array */
if ($paged >= 3) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if (($paged + 2) <= $max) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo \'<div class="pagination"><ul>\' . "\\n";
/** Previous Post Link */
if (get_previous_posts_link(\'«\')) {
printf(\'<li>%s</li>\' . "\\n", get_previous_posts_link(\'«\'));
}
/** Link to first page, plus ellipses if necessary */
if (!in_array( 1, $links)) {
$class = 1 == $paged ? \' class="active"\' : \'\';
printf(\'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url(get_pagenum_link(1)), \'1\');
if (!in_array(2, $links)) {
echo \'<li>…</li>\';
}
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort($links);
foreach ((array) $links as $link) {
$class = $paged == $link ? \' class="active"\' : \'\';
printf(\'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url(get_pagenum_link($link )), $link);
}
/** Link to last page, plus ellipses if necessary */
if (!in_array($max, $links)) {
if (!in_array($max - 1, $links)) {
echo \'<li>…</li>\' . "\\n";
}
$class = $paged == $max ? \' class="active"\' : \'\';
printf(\'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url(get_pagenum_link($max)), $max);
}
/** Next Post Link */
if (get_next_posts_link(\'»\', $max)) {
printf(\'<li>%s</li>\' . "\\n", get_next_posts_link(\'»\', $max));
}
echo \'</ul></div>\' . "\\n";
}
在具有特色帖子的分类页面上,
$posts_per_page
设置为6。
它只显示6篇文章,但分页查询仍然认为每页有10篇文章。
有214篇帖子,但应该只有35页,但如果我转到第22页之后的任何一页,我会得到404。这告诉我它仍在使用posts per page
在WordPress admin中的Reading settings
.
如果我把它改为“6”--一切都是完美的玫瑰色。但是,我不希望将默认值设置为6。我想通过posts_per_page
自定义查询中的变量。
你知道为什么会这样,或者我这里有什么错吗?我真的很头疼。