这是我使用的分页函数。我不确定github链接是否是原始信用,但我找到了它。将以下内容添加到函数中。php文件:
/**
* Creates Custom Pagination
* @link https://gist.github.com/rgfx/755cfb71fd1732e4e7b7bdcbd4b6e4e3
* @param string $numpages [description]
* @param string $pagerange [description]
* @param string $paged [description]
* @return html
*/
function custom_pagination($numpages = \'\', $pagerange = \'\', $paged=\'\') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
* This first part of our function is a fallback
* for custom pagination inside a regular loop that
* uses the global $paged and global $wp_query variables.
*
* It\'s good because we can now override default pagination
* in our theme, and use this function in default quries
* and custom queries.
*/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == \'\') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
* We construct the pagination arguments to enter into our paginate_links
* function.
*/
$pagination_args = array(
\'base\' => get_pagenum_link(1) . \'%_%\',
\'format\' => \'page/%#%\',
\'total\' => $numpages,
\'current\' => $paged,
\'show_all\' => False,
\'end_size\' => 1,
\'mid_size\' => $pagerange,
\'prev_next\' => True,
\'prev_text\' => __(\'\'),
\'next_text\' => __(\'\'),
\'type\' => \'plain\',
\'add_args\' => false,
\'add_fragment\' => \'\'
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class=\'custom-pagination\'>";
echo "<span class=\'page-numbers page-num\'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}\'
然后,在存档模板中,在wp_reset_postdata(); :
<!-- pagination here -->
<?php if (function_exists(custom_pagination)) {
custom_pagination($post_query->max_num_pages,"",$paged);
} ?>
此外,这可能取决于您所使用的页面模板。Im采用自定义模板或归档页面模板。
接下来,如果您仍然有问题,那么我建议检查$paged变量,如下所示:
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$suri = $_SERVER[\'REQUEST_URI\'];
// check $paged variable
echo \'<pre style="display:none; text-align: left; overflow:visible!important; z-index: 10000; position: relative;">\';
print_r($paged);
echo \'</pre>\';
// grab the last number from the url to use as the $paged variable
$exp_suri = explode(\'/\',$suri);
$page_query = $exp_suri[2];
$page_num = substr($page_query, -1);
$paged = is_numeric($page_num) ? $page_num : $paged;
// Now, check $paged variable again
echo \'<pre style="display:none; text-align: left; overflow:visible!important; z-index: 10000; position: relative;">\';
print_r($paged);
echo \'</pre>\';
您可能可以删除或注释掉第二部分(结尾是exp\\u suri)。这里的前置标签是
display:none, 因此,要么将其从内联样式中去掉,要么使用开发工具显示它。
最后,如果你还卡住了,我会读WordPress pagination trouble shooting, here.