固定鏈接設(shè)為 /archives/%postname%.html 時(shí)可以讓頁面看起來像靜態(tài)頁,同時(shí)會(huì)使分頁鏈接變得十分奇怪,比如評(píng)論的分頁鏈接會(huì)變成”hello-world.html/comment-page-1#comments”,html既然是后綴就應(yīng)該一直在最后,本文介紹如何實(shí)現(xiàn)。
目標(biāo)
假設(shè)頁面鏈接為hello-world.html
當(dāng)在文章中插入分頁時(shí),希望分頁鏈接格式為 hello-world/page-2.html
評(píng)論分頁鏈接則為 hello-world/comment-page-2.html
實(shí)現(xiàn)方法
- 通過filter將分頁鏈接改成希望的格式,分別用到vwp_link_pages_link 和 get_comments_pagenum_link。
- 添加自定義跳轉(zhuǎn)規(guī)則,利用filter rewrite_rules_array
- 取消Canonical URL(標(biāo)準(zhǔn)鏈接)跳轉(zhuǎn),否則使用新鏈接訪問時(shí)WordPress會(huì)強(qiáng)制跳轉(zhuǎn)到原來的鏈接
代碼
下面這段代碼放在主題的functions.php中,保存后需要到設(shè)置中重新保存一下固定鏈接。
class Rewrite_Inner_Page_Links{
var $separator;
var $post_rule;
var $comment_rule;
function __construct(){
$this->separator = '/page-';
$this->post_rule = 'archives/([^/]+)('.$this->separator.'([0-9]+))?.html/?$';
$this->comment_rule = 'archives/([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$';
if( !is_admin() || defined( 'DOING_AJAX' ) ) :
add_filter( 'wp_link_pages_link', array( $this, 'inner_page_link_format' ), 10, 2 ); // for inner pages
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' ) );
endif;
}
/**
* 修改post分頁鏈接的格式
* @param string $link
* @param int $number
* @return string
*/
function inner_page_link_format( $link, $number ){
if( $number > 1 ){
if( preg_match( '%<a href=".*\.html/\d*"%', $link ) ){
$link = preg_replace( "%(\.html)/(\d*)%", $this->separator."$2$1", $link );
}
}
return $link;
}
/**
* 修改評(píng)論分頁鏈接
* @param string $result
* @return string
*/
function comment_page_link_format( $result ){
// From hello-world.html/comment-page-1#comments to hello-world/comment-page-1.html#comments
if( strpos( $result, '.html/' ) !== false ){
$result = preg_replace( '=([^/]+)(.html)/comment-page-([0-9]{1,})=', "$1/comment-page-$3$2" ,$result );
}
return $result;
}
/**
* 為新的鏈接格式增加重定向規(guī)則,移除原始分頁鏈接的重定向規(guī)則,防止重復(fù)收錄
*
* 訪問原始鏈接將返回404
* @param array $rules
* @return array
*/
function pagelink_rewrite_rules( $rules ){
foreach ($rules as $rule => $rewrite) {
if ( $rule == '([^/]+).html(/[0-9]+)?/?$' || $rule == '([^/]+).html/comment-page-([0-9]{1,})/?$' ) {
unset($rules[$rule]);
}
}
$new_rule[ $this->post_rule ] = 'index.php?name=$matches[1]&page=$matches[3]';
$new_rule[ $this->comment_rule ] = 'index.php?name=$matches[1]&cpage=$matches[2]';
return $new_rule + $rules;
}
/**
* 禁止WordPress將頁面分頁鏈接跳轉(zhuǎn)到原來的格式
* @param string $redirect_url
* @param string $requested_url
* @return bool
*/
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();
本代碼適用于固定鏈接格式為/archives/%postname%.html,若固定格式不同需要作相應(yīng)修改,修改方法見下文。
若固定鏈接格式為/%postname%.html,請(qǐng)修改規(guī)則,將
$this->post_rule = 'archives/([^/]+)('.$this->separator.'([0-9]+))?.html/?$';
$this->comment_rule = 'archives/([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$';
改為
$this->post_rule = '([^/]+)('.$this->separator.'([0-9]+))?.html/?$';
$this->comment_rule = '([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$';
本文介紹的方法演示了修改固定鏈接格式、并添加新Rewrite rules的方法,適用于其他情況。例如修改custom post type的固定鏈接,不同的是用哪個(gè)filter來修改鏈接輸出格式。
原文出自:http://www.solagirl.net/permalink-with-html-suffix.html,感謝原作者 Sola!





http://www.ydqwiac.cn/permalink-with-html-suffix.html/333 博主你這個(gè)網(wǎng)址加任何后綴都能打開,不對(duì)頭啊
經(jīng)測(cè)試,文章分頁沒問題,但是評(píng)論分頁重寫的網(wǎng)址可以打開,原先的網(wǎng)址也打不開了。
但是,網(wǎng)頁底部的評(píng)論分頁鏈接卻還是原來的,沒有被替換成新鏈接,是不是鉤子不對(duì)呢?能否修正一下,太感謝了!
如果是設(shè)置的postid.html這樣怎么辦,謝謝!
原來是這個(gè)樣子的
謝了!
post_id怎么改呢。。。。
哎 第二頁就404了 ?
? 對(duì)于一些主題好像不起作用吧
直接用插件就解決 了··
有啥插件
本文挺好,為啥沒人評(píng)論?