具體的實(shí)現(xiàn)方法是:
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.然后在頁(yè)面相應(yīng)位置,把原來(lái)的分頁(yè)導(dǎo)航代碼改成以下:
<?php pagenavi(); ?>
這樣就已經(jīng)實(shí)現(xiàn)了分頁(yè)導(dǎo)航,但此時(shí)的分頁(yè)導(dǎo)航還很難看,頁(yè)碼都擠在一起,所以我們需要在style.css中進(jìn)行適當(dāng)裝飾:
/* 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;
}
當(dāng)然,這里只是簡(jiǎn)單的裝飾,你可以根據(jù)主題風(fēng)格做必要的修改。
更多信息請(qǐng)查看IT技術(shù)專欄