WordPress主題中添加文章列表頁(yè)頁(yè)碼導(dǎo)航的PHP代碼實(shí)例
WordPress 默認(rèn)給主題開發(fā)者的建議是在文章列表底部提供上下頁(yè)按鈕,所以沒有提供直接用在文章列表下的分頁(yè)導(dǎo)航的函數(shù)。這里我提供一個(gè)比較完善的分頁(yè)導(dǎo)航函數(shù)。

/**
*WordPress 文章列表分頁(yè)導(dǎo)航
*http://www.endskin.com/page-navi/
*/
function Bing_get_pagenavi( $query = false, $num = false, $before = '<article class="pagenavi postlistpagenavi">', $after = '</article>', $options = array() ){
global $wp_query;
$options = wp_parse_args( $options, array(
'pages_text' => '%CURRENT_PAGE%/%TOTAL_PAGES%',
'current_text' => '%PAGE_NUMBER%',
'page_text' => '%PAGE_NUMBER%',
'first_text' => __( '« 首頁(yè)', 'Bing' ),
'last_text' => __( '尾頁(yè) »', 'Bing' ),
'next_text' => __( '»', 'Bing' ),
'prev_text' => '«',
'dotright_text' => '...',
'dotleft_text' => '...',
'num_pages' => 5,
'always_show' => 0,
'num_larger_page_numbers' => 3,
'larger_page_numbers_multiple' => 10
) );
if( $wp_query->max_num_pages <= 1 || is_single() ) return;
if( !empty( $query ) ){
$request = $query->request;
$numposts = $query->found_posts;
$max_page = $query->max_num_pages;
$posts_per_page = intval( $num );
}else{
$request = $wp_query->request;
$numposts = $wp_query->found_posts;
$max_page = $wp_query->max_num_pages;
$posts_per_page = intval( get_query_var( 'posts_per_page' ) );
}
$paged = intval( get_query_var( 'paged' ) );
if( empty( $paged ) || $paged == 0 ) $paged = 1;
$pages_to_show = intval( $options['num_pages'] );
$larger_page_to_show = intval( $options['num_larger_page_numbers'] );
$larger_page_multiple = intval( $options['larger_page_numbers_multiple'] );
$pages_to_show_minus_1 = $pages_to_show - 1;
$half_page_start = floor( $pages_to_show_minus_1 / 2 );
$half_page_end = ceil( $pages_to_show_minus_1 / 2 );
$start_page = $paged - $half_page_start;
if( $start_page <= 0 ) $start_page = 1;
$end_page = $paged + $half_page_end;
if( ( $end_page - $start_page ) != $pages_to_show_minus_1 ) $end_page = $start_page + $pages_to_show_minus_1;
if( $end_page > $max_page ){
$start_page = $max_page - $pages_to_show_minus_1;
$end_page = $max_page;
}
if( $start_page <= 0 ) $start_page = 1;
$larger_per_page = $larger_page_to_show * $larger_page_multiple;
$larger_start_page_start = ( ( floor( $start_page / 10 ) * 10 ) + $larger_page_multiple ) - $larger_per_page;
$larger_start_page_end = floor( $start_page / 10 ) * 10 + $larger_page_multiple;
$larger_end_page_start = floor( $end_page / 10 ) * 10 + $larger_page_multiple;
$larger_end_page_end = floor( $end_page / 10 ) * 10 + ( $larger_per_page );
if( $larger_start_page_end - $larger_page_multiple == $start_page ){
$larger_start_page_start = $larger_start_page_start - $larger_page_multiple;
$larger_start_page_end = $larger_start_page_end - $larger_page_multiple;
}
if( $larger_start_page_start <= 0 ) $larger_start_page_start = $larger_page_multiple;
if( $larger_start_page_end > $max_page ) $larger_start_page_end = $max_page;
if( $larger_end_page_end > $max_page ) $larger_end_page_end = $max_page;
if( $max_page > 1 || intval( $options['always_show'] ) == 1 ){
$pages_text = str_replace( '%CURRENT_PAGE%', number_format_i18n( $paged ), $options['pages_text'] );
$pages_text = str_replace( '%TOTAL_PAGES%', number_format_i18n( $max_page ), $pages_text);
echo $before;
if( !empty( $pages_text ) ) echo '<span class="pages">' . $pages_text . '</span>';
if( $start_page >= 2 && $pages_to_show < $max_page ){
$first_page_text = str_replace( '%TOTAL_PAGES%', number_format_i18n( $max_page ), $options['first_text'] );
echo '<a href="' . esc_url( get_pagenum_link() ) . '" class="first" title="' . $first_page_text . '">' . $first_page_text . '</a>';
}
if( $larger_page_to_show > 0 && $larger_start_page_start > 0 && $larger_start_page_end <= $max_page ){
for( $i = $larger_start_page_start;$i < $larger_start_page_end;$i += $larger_page_multiple ){
$page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['page_text'] );
echo '<a href="' . esc_url( get_pagenum_link( $i ) ) . '" class="page" title="' . $page_text . '">' . $page_text . '</a>';
}
}
previous_posts_link( $options['prev_text'] );
for( $i = $start_page;$i <= $end_page;$i++ ){
if( $i == $paged ){
$current_page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['current_text'] );
echo '<span class="current">' . $current_page_text . '</span>';
}else{
$page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['page_text'] );
echo '<a href="' . esc_url( get_pagenum_link( $i ) ).'" class="page" title="' . $page_text . '">' . $page_text . '</a>';
}
}
if( empty( $query ) ) echo '<span id="next-page">';
next_posts_link( $options['next_text'], $max_page );
if( empty( $query ) ) echo '</span>';
}
if( $larger_page_to_show > 0 && $larger_end_page_start < $max_page ){
for( $i = $larger_end_page_start;$i <= $larger_end_page_end;$i += $larger_page_multiple ){
$page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['page_text'] );
echo '<a href="' . esc_url( get_pagenum_link( $i ) ).'" class="page" title="' . $page_text . '">' . $page_text . '</a>';
}
}
if( $end_page < $max_page ){
$last_page_text = str_replace( '%TOTAL_PAGES%', number_format_i18n( $max_page ), $options['last_text'] );
echo '<a href="' . esc_url( get_pagenum_link( $max_page ) ) . '" class="last" title="' . $last_page_text . '">' . $last_page_text . '</a>';
}
echo $after;
}
然后在需要使用分頁(yè)導(dǎo)航的地方添加下邊的代碼:
<?php if( function_exists( 'Bing_get_pagenavi' ) ) Bing_get_pagenavi(); ?>
- WordPress主題制作中自定義頭部的相關(guān)PHP函數(shù)解析
- WordPress主題制作之模板文件的引入方法
- WordPress的主題編寫中獲取頭部模板和底部模板
- WordPress中使主題支持小工具以及添加插件啟用函數(shù)
- 實(shí)現(xiàn)WordPress主題側(cè)邊欄切換功能的PHP腳本詳解
- 編寫PHP腳本使WordPress的主題支持Widget側(cè)邊欄
- WordPress中編寫自定義存儲(chǔ)字段的相關(guān)PHP函數(shù)解析
- WordPress中給文章添加自定義字段及后臺(tái)編輯功能區(qū)域
- 在WordPress中獲取數(shù)據(jù)庫(kù)字段內(nèi)容和添加主題設(shè)置菜單
相關(guān)文章
Laravel框架實(shí)現(xiàn)利用中間件進(jìn)行操作日志記錄功能
這篇文章主要介紹了Laravel框架實(shí)現(xiàn)利用中間件進(jìn)行操作日志記錄功能,結(jié)合實(shí)例形式分析了Laravel框架中間件的創(chuàng)建、引入以及使用中間件進(jìn)行操作日志記錄功能的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06
PHP Ajax JavaScript Json獲取天氣信息實(shí)現(xiàn)代碼
這篇文章主要介紹了PHP Ajax JavaScript Json實(shí)現(xiàn)天氣信息獲取 的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-08-08
php+js實(shí)現(xiàn)異步圖片上傳實(shí)例分享
本來(lái)想用插件上傳圖片的,后來(lái)自己寫了一個(gè)簡(jiǎn)單的js實(shí)現(xiàn)異步的圖片上傳,不多說(shuō)上代碼非常簡(jiǎn)單2014-06-06
php簡(jiǎn)單的留言板與回復(fù)功能具體實(shí)現(xiàn)
留言板是在剛接觸php時(shí)用來(lái)學(xué)習(xí)的一個(gè)簡(jiǎn)單的應(yīng)用例子了,今天我再給初學(xué)php的朋友提供一個(gè)完整的php留言板的全部制作過(guò)程,希望對(duì)你會(huì)有幫助2014-02-02
thinkPHP實(shí)現(xiàn)基于ajax的評(píng)論回復(fù)功能
這篇文章主要介紹了thinkPHP實(shí)現(xiàn)基于ajax的評(píng)論回復(fù)功能,結(jié)合實(shí)例形式分析了thinkPHP實(shí)現(xiàn)ajax評(píng)論回復(fù)所涉及的控制器、視圖、樣式、js使用post進(jìn)行ajax提交、并附帶了相應(yīng)的SQL語(yǔ)句,需要的朋友可以參考下2018-06-06
php項(xiàng)目中百度 UEditor 簡(jiǎn)單安裝調(diào)試和調(diào)用
這篇文章主要介紹了php項(xiàng)目中百度 UEditor 簡(jiǎn)單安裝調(diào)試和調(diào)用的相關(guān)資料,需要的朋友可以參考下2015-07-07
CodeIgniter擴(kuò)展核心類實(shí)例詳解
這篇文章主要介紹了CodeIgniter擴(kuò)展核心類,結(jié)合實(shí)例形式分析了CodeIgniter針對(duì)核心類的擴(kuò)展方法與擴(kuò)展CI類庫(kù)與輔助函數(shù)的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01

