我正在使用以下代码进行分页。
// Pagination
function my_paginate_links() {
global $wp_rewrite, $wp_query;
$wp_query->query_vars[\'paged\'] > 1 ? $current = $wp_query->query_vars[\'paged\'] : $current = 1;
$pagination = array(
\'base\' => @add_query_arg(\'page\',\'%#%\'),
\'format\' => \'\',
\'total\' => $wp_query->max_num_pages,
\'current\' => $current,
\'prev_text\' => __(\'\'),
\'next_text\' => __(\'next »\'),
\'end_size\' => 1,
\'mid_size\' => 2,
\'show_all\' => true,
\'type\' => \'list\'
);
if ( $wp_rewrite->using_permalinks() )
$pagination[\'base\'] = user_trailingslashit( trailingslashit( remove_query_arg( \'s\', get_pagenum_link( 1 ) ) ) . \'page/%#%/\', \'paged\' );
if ( !empty( $wp_query->query_vars[\'s\'] ) )
$pagination[\'add_args\'] = array( \'s\' => get_query_var( \'s\' ) );
echo paginate_links( $pagination );
}
当它位于第一页时,“上一页”链接不会显示。我理解为什么,但我希望它能显示出来,因为我是以某种方式设计网站的。以上代码中是否有一部分我可以更改以实现这一点?我已经通读了
codex 关于这一点,但找不到对我有意义的答案。如果有任何指导,我将不胜感激。非常感谢。
SO网友:Geert
查看paginate_links()
似乎没有选项可以始终包含上一个或下一个链接。该函数只是将当前页码与总页码进行比较,以确定是否需要添加这些链接。
不过,解决这个问题是可能的。这应该让您开始:
$page_links = paginate_links(array(
// Make the function never return previous or next links,
// we will add them manually.
\'prev_next\' => FALSE,
// Give us back an array, this is the easiest to work with.
\'type\' => \'array\',
// + all your other arguments here
));
// Now it is just a matter of adding a previous and next link manually.
// Note: you still have to build the actual prev/next URLs.
$prev_link = \'<a href="#">\'.__(\'Previous\').\'</a>\';
$next_link = \'<a href="#">\'.__(\'Next\').\'</a>\';
array_unshift($page_links, $prev_link);
$page_links[] = $next_link;
// Output
echo implode($page_links);
SO网友:Jonathan G. Bastiat
有点晚了,但我只是遇到了这个问题,发现了您尚未解决的问题。这就是我想要的。。。我们可以获得一个链接数组,因此,如果我们比较当前的页码,我们可以手动添加上一个/下一个链接,并循环其余链接:
// Pagination
function my_paginate_links() {
global $wp_rewrite, $wp_query;
$wp_query->query_vars[\'paged\'] > 1 ? $current = $wp_query->query_vars[\'paged\'] : $current = 1;
$pagination = array(
\'base\' => @add_query_arg(\'page\',\'%#%\'),
\'format\' => \'\',
\'total\' => $wp_query->max_num_pages,
\'current\' => $current,
\'prev_text\' => __(\'\'),
\'next_text\' => __(\'next »\'),
\'end_size\' => 1,
\'mid_size\' => 2,
\'show_all\' => true,
\'type\' => \'array\'
);
if ( $wp_rewrite->using_permalinks() )
$pagination[\'base\'] = user_trailingslashit( trailingslashit( remove_query_arg( \'s\', get_pagenum_link( 1 ) ) ) . \'page/%#%/\', \'paged\' );
if ( !empty( $wp_query->query_vars[\'s\'] ) )
$pagination[\'add_args\'] = array( \'s\' => get_query_var( \'s\' ) );
$pages = paginate_links( $pagination );
echo \'<ul>\';
if ( $paged == 1) echo \'<li><a href="#" class="disabled">«</a></li>\';
foreach ($pages as $page) :
echo \'<li>\'.$page.\'</li>\';
endforeach;
if ( $paged == $wp_query->max_num_pages ) echo \'<li><a href="#" class="disabled">»</a></li>\';
echo \'</ul>\';
}