WordPress 如何實現分頁導航?
想自己實現的話...直接看 WP-PageNavi 的源碼就是了。
很容易. 了解下wp的自定義模版就可以了.
可以看看這篇文章:覺唯主題自動翻頁改成手動翻頁,裡面的方法適用於所以的主題。推薦看看。
我想問一下如果不增加插件,如何能實現。。。
wordpress自帶的主題很多都沒有自帶分頁的功能,即便有也很弱,只有更早和更新,我自己使用的主題頁有這個問題,可以使用WP-PageNavi插件,可以對顯示效果進行調整。推薦使用。
我的是用以下代碼實現的:
/*分頁代碼*/
/* Function that Rounds To The Nearest Value.
Needed for the pagenavi() function */
function round_num($num, $to_nearest) {
/*Round fractions down (PHP: floor - Manual)*/
return floor($num/$to_nearest)*$to_nearest;
}
/* Function that performs a Boxed Style Numbered Pagination (also called Page Navigation).
Function is largely based on Version 2.4 of the WP-PageNavi plugin */
function wp_pagenavi($before = , $after = ) {
global $wpdb, $wp_query;
$pagenavi_options = array();
$pagenavi_options[pages_text] = ();
$pagenavi_options[current_text] = %PAGE_NUMBER%;
$pagenavi_options[page_text] = %PAGE_NUMBER%;
$pagenavi_options[first_text] = ();
$pagenavi_options[last_text] = ();
$pagenavi_options[next_text] = #8250;;
$pagenavi_options[prev_text] = #8249; ;
$pagenavi_options[dotright_text] = ...;
$pagenavi_options[dotleft_text] = ...;
$pagenavi_options[num_pages] = 5; //continuous block of page numbers
$pagenavi_options[always_show] = 0;
$pagenavi_options[num_larger_page_numbers] = 3;
$pagenavi_options[larger_page_numbers_multiple] = 10;
//If NOT a single Post is being displayed
/*Function Reference/is single ? WordPress Codex)*/
if (!is_single()) {
$request = $wp_query-&>request;
//intval — Get the integer value of a variable
/*PHP: intval - Manual*/
$posts_per_page = intval(get_query_var(posts_per_page));
//Retrieve variable in the WP_Query class.
/*Function Reference/get query var ? WordPress Codex*/
$paged = intval(get_query_var(paged));
$numposts = $wp_query-&>found_posts;
$max_page = $wp_query-&>max_num_pages;
//empty — Determine whether a variable is empty
/*PHP: empty - Manual*/
if(empty($paged) || $paged == 0) {
$paged = 1;
}
$pages_to_show = intval($pagenavi_options[num_pages]);
$larger_page_to_show = intval($pagenavi_options[num_larger_page_numbers]);
$larger_page_multiple = intval($pagenavi_options[larger_page_numbers_multiple]);
$pages_to_show_minus_1 = $pages_to_show - 1;
$half_page_start = floor($pages_to_show_minus_1/2);
//ceil — Round fractions up (PHP: ceil - Manual)
$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;
//round_num() custom function - Rounds To The Nearest Value.
$larger_start_page_start = (round_num($start_page, 10) + $larger_page_multiple) - $larger_per_page;
$larger_start_page_end = round_num($start_page, 10) + $larger_page_multiple;
$larger_end_page_start = round_num($end_page, 10) + $larger_page_multiple;
$larger_end_page_end = round_num($end_page, 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($pagenavi_options[always_show]) == 1) {
/*PHP: str_replace */
/*number_format_i18n(): Converts integer number to format based on locale (wp-includes/functions.php*/
$pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), $pagenavi_options[pages_text]);
$pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
echo $before.&.&[.$max_page.]&;
if(!empty($pages_text)) {
echo &.$pages_text.&;
}
//Displays a link to the previous post which exists in chronological order from the current post.
/*Function Reference/previous post link ? WordPress Codex*/
previous_posts_link($pagenavi_options[prev_text]);
if ($start_page &>= 2 $pages_to_show &< $max_page) {
$first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options[first_text]);
//esc_url(): Encodes &< &> " (less than, greater than, ampersand, double quote, single quote).
/*Data Validation ? WordPress Codex*/
//get_pagenum_link():(wp-includes/link-template.php)-Retrieve get links for page numbers.
echo &1&;
if(!empty($pagenavi_options[dotleft_text])) {
echo &.$pagenavi_options[dotleft_text].&;
}
}
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), $pagenavi_options[page_text]);
echo &.$page_text.&;
}
}
for($i = $start_page; $i &<= $end_page; $i++) {
if($i == $paged) {
$current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options[current_text]);
echo &.$current_page_text.&;
} else {
$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options[page_text]);
echo &.$page_text.&;
}
}
next_posts_link($pagenavi_options[next_text], $max_page);
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), $pagenavi_options[page_text]);
echo &.$page_text.&;
}
}
if ($end_page &< $max_page) {
if(!empty($pagenavi_options[dotright_text])) {
echo &.$pagenavi_options[dotright_text].&;
}
$last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options[last_text]);
echo &.$max_page.&;
}
echo &
}
}
}
然後在需要顯示分頁碼的地方使用此函數調用:
&
推薦閱讀:
※wordpress網站如何便捷的使用七牛存儲?
※wordpress最有效的加速方法?
※wordpress無法選擇資料庫?
※WordPress將如何url外鏈圖片設置為特色圖片?
※國內有哪些不錯的WordPress主題?
TAG:WordPress | WordPress主題 |