具体的实现方法是:
1.首先在主题functions.php中加入以下代码:
/* Pagenavi */
function pagenavi( $before = '', $after = '', $p = 2 ) {
if ( is_singular() ) return;
global $wp_query, $paged;
$max_page = $wp_query->max_num_pages;
if ( $max_page == 1 ) return;
if ( empty( $paged ) ) $paged = 1;
echo $before.'<div id="pagenavi">'."n";
echo '<span class="pages">Page: ' . $paged . ' of ' . $max_page . ' </span>';
if ( $paged > 1 ) p_link( $paged - 1, 'Previous Page', '«' );
if ( $paged > $p + 1 ) p_link( 1, 'First Page' );
if ( $paged > $p + 2 ) echo '... ';
for( $i = $paged - $p; $i <= $paged + $p; $i++ ) {
if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<span class='page-numbers current'>{$i}</span>" : p_link( $i );
}
if ( $paged < $max_page - $p - 1 ) echo '... ';
if ( $paged < $max_page - $p ) p_link( $max_page, 'Last Page' );
if ( $paged < $max_page ) p_link( $paged + 1,'Next Page', '»' );
echo '</div>'.$after."n";
}
function p_link( $i, $title = '', $linktype = '' ) {
if ( $title == '' ) $title = "Page {$i}";
if ( $linktype == '' ) { $linktext = $i; } else { $linktext = $linktype; }
echo "<a class='page-numbers' href='", esc_html( get_pagenum_link( $i ) ), "' title='{$title}'>{$linktext}</a>";
}
2.然后在页面相应位置,把原来的分页导航代码改成以下:
<?php pagenavi(); ?>
这样就已经实现了分页导航,但此时的分页导航还很难看,页码都挤在一起,所以我们需要在style.css中进行适当装饰:
/* pagenavi */
#pagenavi a, #pagenavi a:visited, #pagenavi span {
height: 25px;
line-height: 25px;
display: inline-block;
padding: 1px 8px;
}
#pagenavi a, #pagenavi a:visited {
margin: 0 2px;
}
#pagenavi span.pages {
color: #777;
font-weight: bold;
margin-right: 10px;
padding: 0;
}
#pagenavi span.current {
margin: -2px 2px -1px;
padding: 0 9px;
height: 28px;
line-height: 28px;
text-align: center;
}
当然,这里只是简单的装饰,你可以根据主题风格做必要的修改。
更多信息请查看IT技术专栏