国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

WordPress用html做網(wǎng)頁后綴時分頁鏈接方法

2024-09-07 00:49:35
字體:
供稿:網(wǎng)友
這篇文章主要為大家詳細介紹了WordPress用html做網(wǎng)頁后綴時分頁鏈接方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下,有需要的朋友可以收藏方便以后借鑒。

WordPress很好用,不少朋友都喜歡使用WordPress,但是WordPress同樣有很多奇葩的問題和漏洞。漏洞這邊361源碼暫時就不分析了,今天就給專門給大家講講WordPress一個奇葩的問題:我們在使用WordPress的時候,設(shè)置頁面的固定鏈接設(shè)為/archives/%postname%.html這種樣式時可以讓頁面看起來像靜態(tài)頁,但同時也會使分頁鏈接變得十分奇怪,比如評論的分頁鏈接會變成”vevb-world.html/comment-page-1#comments”,html既然是后綴就應該一直在最后,要解決這個問題來看看361資源網(wǎng)是怎么做的吧。

假設(shè)頁面鏈接為vevb-world.html,當在文章中插入分頁時,我們希望分頁鏈接格式為vevb-world/page-2.html,評論分頁鏈接則為 vevb-world/comment-page-2.html。

這里可以通過filter將分頁鏈接改成希望的格式,分別用到wp_link_pages_link和 get_comments_pagenum_link。

首先添加自定義跳轉(zhuǎn)規(guī)則,利用filter rewrite_rules_array取消Canonical URL(標準鏈接)跳轉(zhuǎn),否則使用新鏈接訪問時WordPress會強制跳轉(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;        }        /**          * 修改評論分頁鏈接          * @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ī)則,防止重復收錄          *          * 訪問原始鏈接將返回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,若固定格式不同需要作相應修改:

若固定鏈接格式為/%postname%.html,請修改規(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])?$';

OK,到這里就把WordPress用html做網(wǎng)頁后綴時分頁鏈接方法基本講完了,對有需要的朋友肯定能起到幫助作用。大家可以收藏起來方便以后需要的時候使用。

以上就是WordPress用html做網(wǎng)頁后綴時分頁鏈接方法的全部內(nèi)容,希望對大家的學習和解決疑問有所幫助,也希望大家多多支持武林網(wǎng)。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 石狮市| 镇平县| 花莲县| 和平区| 吉首市| 辽中县| 安福县| 镇原县| 南木林县| 右玉县| 乌拉特中旗| 延安市| 大邑县| 玛多县| 乐都县| 乐业县| 泰安市| 绥芬河市| 泰和县| 铅山县| 嵩明县| 堆龙德庆县| 蒙自县| 金塔县| 赣州市| 久治县| 东丽区| 大洼县| 宁晋县| 浑源县| 农安县| 芦溪县| 龙里县| 墨脱县| 阳城县| 荥阳市| 南丰县| 仪征市| 绍兴市| 运城市| 汨罗市|