html 分页插件-WordPress使用.html作为url后缀时分页链接问题

2023-09-04 0 6,405 百度已收录

当固定链接设置为/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(标准链接)跳转html 分页插件,否则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( '%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])?$';

本文介绍的方法演示了更改固定链接格式和添加新的Rewrite规则的方法,并且适用于其他情况。 例如html 分页插件,要更改自定义帖子类型的固定链接,区别在于使用过滤器更改链接输出格式。

原文来自: ,感谢原作者Sola!

免责声明:本站所有文章除特别注明或标注外,均为本站原创发表。 未经本站同意,任何个人或组织不得复制、盗用、转载、转载本站内容至任何网站、图书及其他媒体平台。 若本站内容侵犯原作者合法权益,请联系我们处理。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悟空资源网 html html 分页插件-WordPress使用.html作为url后缀时分页链接问题 https://www.wkzy.net/game/193233.html

常见问题

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务