首页 > CMS建站 > Wordpress > 正文

WordPress用html做网页后缀时分页链接方法

2020-07-03 13:07:17
字体:
来源:转载
供稿:网友
这篇文章主要为大家详细介绍了WordPress用html做网页后缀时分页链接方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴。

WordPress很好用,不少朋友都喜欢使用WordPress,但是WordPress同样有很多奇葩的问题和漏洞。漏洞这边361源码暂时就不分析了,今天就给专门给大家讲讲WordPress一个奇葩的问题:我们在使用WordPress的时候,设置页面的固定链接设为/archives/%postname%.html这种样式时可以让页面看起来像静态页,但同时也会使分页链接变得十分奇怪,比如评论的分页链接会变成”cuoxin-world.html/comment-page-1#comments”,html既然是后缀就应该一直在最后,要解决这个问题来看看361资源网是怎么做的吧。

假设页面链接为cuoxin-world.html,当在文章中插入分页时,我们希望分页链接格式为cuoxin-world/page-2.html,评论分页链接则为 cuoxin-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做网页后缀时分页链接方法的全部内容,希望对大家的学习和解决疑问有所帮助,也希望大家多多支持错新网。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表