WordPress默认翻页功能很弱,一页页翻,弄到你手麻,所以类似wp-pagenavi翻页插件,几乎是WordPress博客必装的插件,也是普及率很高的一款插件。
虽然网上有如何把wp-pagenavi插件集成到WordPress主题的教程,但wp-pagenavi插件函数代码拖沓,只是变相启用插件而已,并没有真正脱离wp-pagenavi插件。
下面有2种方法,都是用一段轻量级的函数来实现分页插件的功能。
WordPress 现在已经自己支持 Pagenavi 功能了,首先要在functions.php
文件中加入以下代码
/*@分页功能*/ function native_pagenavi(){ global $wp_query, $wp_rewrite; $wp_query->query_vars["paged"] > 1 ? $current = $wp_query->query_vars["paged"] : $current = 1; $pagination = array( "base" => @add_query_arg("page","%#%"), "format" => "", "total" => $wp_query->max_num_pages, "current" => $current, "prev_text" => "« ", "next_text" => " »" ); if( $wp_rewrite->using_permalinks() ) $pagination["base"] = user_trailingslashit( trailingslashit( remove_query_arg("s",get_pagenum_link(1) ) ) . "page/%#%/", "paged"); if( !empty($wp_query->query_vars["s"]) ) $pagination["add_args"] = array("s"=>get_query_var("s")); echo paginate_links($pagination); }
在需要调用的位置插入以下代码即可实现 Pagenavi 的功能。
更多参考官方
.pagenav{ clear:both; padding-top:9px; } .pagenav span,.pagenav a{ font-size:14px; margin: 2px 6px 2px 0; background:#fff; border:1px solid #e5e5e5; color:#787878; padding:3px 8px; text-decoration:none; } .pagenav a:hover{ background: #8391A7; border:1px solid #fff; color:#fff; } .pagenav .current{ border:1px solid #8d8d8d; color:#393939 }
/*@pagenav*/ .pagenav{ text-align:center; padding:1em; clear:both; } .page-numbers{ border-bottom:5px solid #ccc; padding:5px 8px 4px; margin:0 5px; } .page-numbers:hover,.pagenav .current{ color:#000; border-bottom:5px solid #000; }
functions.php中加入
function pagination($query_string){ global $posts_per_page, $paged; $my_query = new WP_Query($query_string ."&posts_per_page=-1"); $total_posts = $my_query->post_count; if(empty($paged))$paged = 1; $prev = $paged - 1; $next = $paged + 1; $range = 2; // only edit this if you want to show more page-links $showitems = ($range * 2)+1;$pages = ceil($total_posts/$posts_per_page); if(1 != $pages){ echo ""; echo ($paged > 2 && $paged+$range+1 > $pages && $showitems < $pages)? "最前":""; echo ($paged > 1 && $showitems < $pages)? "上一页":"";for ($i=1; $i <= $pages; $i++){ if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){ echo ($paged == $i)? "".$i."":"".$i.""; } }echo ($paged < $pages && $showitems < $pages) ? "下一页" :""; echo ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) ? "最后":""; echo "\n"; } }
在需要调用分页的位置用以下代码
.pagination{ line-height:23px; } .pagination span, .pagination a{ font-size:12px; margin: 2px 6px 2px 0; background:#fff; border:1px solid #e5e5e5; color:#787878; padding:2px 5px 2px 5px; text-decoration:none; } .pagination a:hover{ background: #8391A7; border:1px solid #fff; color:#fff; } .pagination .current{ background: #fff; border:1px solid #8d8d8d; color:#393939; font-size:12px; padding:2px 5px 2px 5px; }
根据自己的主题调整CSS即可。
欢迎转载,但请保留原文地址 http://www.sjyhome.com/wordpress/pagenavi.html