php 分页 url重写 分页问题,解决千古难题,wordpress分页URL问题,wordpress给分页加链接...
原本的wordpress文章如果分頁(yè)會(huì)成如下的結(jié)構(gòu):
http://www.xyqzmt.cn/1.html/2
通常固定鏈接都是這樣的結(jié)構(gòu),設(shè)為/%postname%.html或者/%post_id%.html 以前我一直無(wú)法解決如上的問(wèn)題,最后放棄掙扎,如果遇到很長(zhǎng)的文章全都放在一個(gè)頁(yè)面里面。
今天在知更鳥(niǎo)大佬的博客上找到了結(jié)局辦法。http://zmingcx.com/pseudo-static-article-page-links.html
將下面代碼添加到當(dāng)前主題?functions.php中:
本文提供一下/%post_id%.html的修改方法。
// 適合/%post_id%.html分頁(yè)鏈接修正
class Rewrite_Inner_Page_Links_id{
var $separator;
function __construct(){
$this->separator = ‘/page-‘;
if( !is_admin() || defined( ‘DOING_AJAX’ ) ) :
add_filter( ‘wp_link_pages_link’, array( $this, ‘inner_page_link_format’ ), 10, 2 );
add_filter( ‘get_comments_pagenum_link’, array( $this, ‘comment_page_link_format’ ) );
add_filter( ‘redirect_canonical’, array( $this, ‘cancel_redirect_for_paged_posts’ ), 10, 2 );
endif;
if( is_admin() ) :
add_filter( ‘rewrite_rules_array’, array( $this, ‘pagelink_rewrite_rules’ ) );
register_activation_hook( __FILE__, array( $this, ‘flush_rewrite_rules’ ) ) ;
register_deactivation_hook( __FILE__, array( $this, ‘flush_rewrite_rules’ ) );
endif;
}
function flush_rewrite_rules(){
flush_rewrite_rules();
}
// 修改post分頁(yè)鏈接的格式
function inner_page_link_format( $link, $number ){
if( $number > 1 ){
if( preg_match( ‘%
$link = preg_replace( “%(\.html)/(\d*)%”, $this->separator.”$2$1″, $link );
}
}
return $link;
}
// 為新的鏈接格式增加重定向規(guī)則,移除原始分頁(yè)鏈接的重定向規(guī)則,防止重復(fù)收錄
function pagelink_rewrite_rules( $rules ){
foreach ($rules as $rule => $rewrite) {
if ( $rule == ‘([0-9]+).html(/[0-9]+)?/?$’ ) {
unset($rules[$rule]);
}
}
$new_rule[‘([0-9]+)(‘.$this->separator.'([0-9]+))?.html/?$’] = ‘index.php?p=$matches[1]&page=$matches[3]’;
return $new_rule + $rules;
}
// 禁止WordPress將頁(yè)面分頁(yè)鏈接跳轉(zhuǎn)到原來(lái)的格式
function cancel_redirect_for_paged_posts( $redirect_url, $requested_url ){
global $wp_query;
if( is_single() && $wp_query->get( ‘page’ ) > 1 ){
return false;
}
return true;
}
}
new Rewrite_Inner_Page_Links_id();
提示:添加代碼后,需要保存一下固定鏈接設(shè)置。
之后再次打開(kāi)文章分頁(yè)鏈接,會(huì)變成類似的:
/morning-paper-news/page-2.html
/132/page-2.html
解決了我的千古難題,我弄的那個(gè)漫畫(huà)教程網(wǎng)站,復(fù)制貼吧里面的教程,有大量的圖片,起碼要分個(gè)七八頁(yè)才OK的。增加網(wǎng)站的PV也挺好的!前提就是內(nèi)容足夠吸引人!等有機(jī)會(huì)再研究那種能整頁(yè)展示分頁(yè)的功能。(參考太平洋電腦網(wǎng)上的那種)
總結(jié)
以上是生活随笔為你收集整理的php 分页 url重写 分页问题,解决千古难题,wordpress分页URL问题,wordpress给分页加链接...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: GhostTray.exe是什么进程 有
- 下一篇: 民生信用卡2017优惠 6月份可参与优惠