我正在使用 paginate_links。
现在我有:
Page 1 of 6
1, 2, 3, 4, 5, ... »
Page 3 of 6
«
1, 2, 3, 4, 5, ... »
Page 6 of 6
«
..., 2, 3, 4, 5, 6
<div class="pagination">
<span class="pages">
<?php if( empty( $var ) ) {
global $wp_query;
if( !isset( $wp_query->max_num_pages ) )
return;
$pages = $wp_query->max_num_pages;
}
else {
global $$var;
if( !is_a( $$var, 'WP_Query' ) )
return;
if( !isset( $$var->max_num_pages ) || !isset( $$var ) )
return;
$pages = absint( $$var->max_num_pages );
}
if( $pages < 1 )
return;
$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
echo 'Page ' . $page . ' of ' . $pages; ?>
</span>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'mid_size' => 2,
'prev_text' => __('«'),
'next_text' => __('»'),
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
) );
?>
</div>
如何添加
"last"
和"first"
链接来实现以下分页样式?
Page 1 of 20
1, 2, 3, 4, 5, ... »
Last »
Page 10 of 20
« First
«
10, 11, 12, 13, 14, ... »
Last »
Page 20 of 20
« First
«
..., 16, 17, 18, 19, 20
尝试将此功能添加到活动主题的functions.php文件中
function tutsocean_pagination( $range = 5 ) {
global $paged, $wp_query;
if ( !$max_page )
$max_page = $wp_query->max_num_pages;
if ( $max_page > 1 )
if ( !$paged ) $paged = 1;
echo "\n".'<ul class="dcs_pagination">'."\n";
if ( $paged != 1 )
echo '<li class="page-num page-num-first"><a href='.get_pagenum_link(1).'>'.__('« First', "PAGE_LANG").' </a></li>';
echo '<li class="page-num page-num-prev">';
previous_posts_link(' « ');
echo '</li>';
if ( $max_page > $range ) :
if ( $paged < $range ) :
for ( $i = 1; $i <= ($range + 1); $i++ ) {
$class = $i == $paged ? 'current' : '';
echo '<li class="page-num"><a class="paged-num-link '.$class.'" href="'.get_pagenum_link($i).'"> '.$i.' </a></li>';
}
elseif ( $paged >= ( $max_page - ceil($range/2)) ) :
for ( $i = $max_page - $range; $i <= $max_page; $i++ ){
$class = $i == $paged ? 'current' : '';
echo '<li class="page-num"><a class="paged-num-link '.$class.'" href="'.get_pagenum_link($i).'"> '.$i.' </a></li>';
}
endif;
elseif ( $paged >= $range && $paged < ( $max_page - ceil($range/2)) ) :
for ( $i = ($paged - ceil($range/2)); $i <= ($paged + ceil($range/2)); $i++ ) {
$class = $i == $paged ? 'current' : '';
echo '<li class="page-num"><a class="paged-num-link '.$class.'" href="'.get_pagenum_link($i).'"> '.$i.' </a></li>';
}
else :
for ( $i = 1; $i <= $max_page; $i++ ) {
$class = $i == $paged ? 'current' : '';
echo '<li class="page-num"><a class="paged-num-link '.$class.'" href="'.get_pagenum_link($i).'"> '.$i.' </a></li>';
}
endif;
echo '<li class="page-num page-num-next">';
next_posts_link(' » ');
echo '</li>';
if ( $paged != $max_page )
echo '<li class="page-num page-num-last"><a href='.get_pagenum_link($max_page).'> '.__('Last »', "PAGE_LANG").'</a></li>';
echo "\n".'</ul>'."\n";
}
之后,在要显示分页的地方添加以下代码。
<div class="pagination">
<span class="pages">
<?php if( empty( $var ) ) {
global $wp_query;
if( !isset( $wp_query->max_num_pages ) )
return;
$pages = $wp_query->max_num_pages;
}
else {
global $$var;
if( !is_a( $$var, 'WP_Query' ) )
return;
if( !isset( $$var->max_num_pages ) || !isset( $$var ) )
return;
$pages = absint( $$var->max_num_pages );
}
if( $pages < 1 )
return;
$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
echo 'Page ' . $page . ' of ' . $pages; ?>
</span>
<?php tutsocean_pagination(); ?>
</div>
一些可选的 CSS。如果您想添加自己的 CSS/样式,请忽略
ul.dcs_pagination {list-style: none;display: flex;}
.pagination {
display: flex;
}
ul.dcs_pagination li {
padding: 0px 10px;
}
此代码最好的部分是您可以更改
tutsocean_pagination()
函数定义中的下一个上一个按钮或第一个最后一个按钮