WordPress很好用,不少朋友都喜歡使用WordPress,但是WordPress同樣有很多奇葩的問題和漏洞。漏洞這邊361源碼暫時就不分析了,今天就給專門給大家講講WordPress一個奇葩的問題:我們在使用WordPress的時候,設置頁面的固定鏈接設為/archives/%postname%.html這種樣式時可以讓頁面看起來像靜態頁,但同時也會使分頁鏈接變得十分奇怪,比如評論的分頁鏈接會變成”vevb-world.html/comment-page-1#comments”,html既然是后綴就應該一直在最后,要解決這個問題來看看361資源網是怎么做的吧。
假設頁面鏈接為vevb-world.html,當在文章中插入分頁時,我們希望分頁鏈接格式為vevb-world/page-2.html,評論分頁鏈接則為 vevb-world/comment-page-2.html。
這里可以通過filter將分頁鏈接改成希望的格式,分別用到wp_link_pages_link和 get_comments_pagenum_link。
首先添加自定義跳轉規則,利用filter rewrite_rules_array取消Canonical URL(標準鏈接)跳轉,否則使用新鏈接訪問時WordPress會強制跳轉到原來的鏈接,代碼如下(下面這段代碼放在主題的functions.php中,保存后需要到設置中重新保存一下固定鏈接。):
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,請修改規則,將
$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])?$';
OK,到這里就把WordPress用html做網頁后綴時分頁鏈接方法基本講完了,對有需要的朋友肯定能起到幫助作用。大家可以收藏起來方便以后需要的時候使用。
以上就是WordPress用html做網頁后綴時分頁鏈接方法的全部內容,希望對大家的學習和解決疑問有所幫助,也希望大家多多支持武林網。新聞熱點
疑難解答
圖片精選