本文目錄1目標2實現方法3代碼
固定鏈接設為 /archives/%postname%.html 時可以讓頁面看起來像靜態頁,同時會使分頁鏈接變得十分奇怪,比如迴響的分頁鏈接會變成”hello-world.html/comment-page-1#comments”,html既然是後綴就應該一直在最後,本文介紹如何實現。
目標
假設頁面鏈接為hello-world.html
當在文章中插入分頁時,希望分頁鏈接格式為 hello-world/page-2.html
迴響分頁鏈接則為 hello-world/comment-page-2.html
實現方法
通過filter將分頁鏈接改成希望的格式,分別用到vwp_link_pages_link 和 get_comments_pagenum_link。添加自定義跳轉規則,利用filter rewrite_rules_array取消Canonical URL(標準鏈接)跳轉,否則使用新鏈接訪問時WordPress會強制跳轉到原來的鏈接
代碼
下面這段代碼放在主題的functions.php中,保存後需要到設置中重新保存一下固定鏈接。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
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; } /** * 修改迴響分頁鏈接 * @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; } /** * 為新的鏈接格式增加重定向規則,移除原始分頁鏈接的重定向規則,防止重復收錄 * * 訪問原始鏈接將返回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將頁面分頁鏈接跳轉到原來的格式 * @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(); |
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;
}
/**
* 修改迴響分頁鏈接
* @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;
}
/**
* 為新的鏈接格式增加重定向規則,移除原始分頁鏈接的重定向規則,防止重復收錄
*
* 訪問原始鏈接將返回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將頁面分頁鏈接跳轉到原來的格式
* @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,若固定格式不同需要作相應修改,修改方法見下文。
若固定鏈接格式為/%postname%.html,請修改規則,將
1 2 |
$this->post_rule = 'archives/([^/]+)('.$this->separator.'([0-9]+))?.html/?$'; $this->comment_rule = 'archives/([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$'; |
$this->post_rule = ‘archives/([^/]+)(‘.$this->separator.'([0-9]+))?.html/?$’;
$this->comment_rule = ‘archives/([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$’;
改為
1 2 |
$this->post_rule = '([^/]+)('.$this->separator.'([0-9]+))?.html/?$'; $this->comment_rule = '([^/]+)/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的固定鏈接,不同的是用哪個filter來修改鏈接輸出格式。
原文出自:http://www.solagirl.net/permalink-with-html-suffix.html,感謝原作者 Sola!