WordPress 页面链接添加html后缀新手教程

默认WordPress页面不能实现伪静态链接,比如:https://www.52muban.com/aboutus.html,手动在链接中添加“.html”,会自动转码为"-html",但万能的WordPress,你能想到的功能都会有相应的插件帮你实现。既然用插件可以实现,直接将插件中的代码集成到主题中同样也可以,代码提取自.html on PAGES插件,将下面代码添加主题functions.php中即可。

    // 页面链接添加html后缀
    add_action('init', 'html_page_permalink', -1);
    function html_page_permalink() {
        global $wp_rewrite;
        if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){
            $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
        }
    }

添加后,需要到固定链接设置页面,重新保存一下固定链接设置,否则不会生效。上述代码适合伪静态的固定链接形式使用,比如:

    /%postname%.html
    /%post_id%.html

另外,如果同时使用了“给WordPress分类目录和页面添加斜杠”一文中的代码,还需要将该文中的代码修改为:

    function nice_trailingslashit($string, $type_of_url) {
        if ( $type_of_url != 'single' && $type_of_url != 'page' && $type_of_url != 'single_paged' )
            $string = trailingslashit($string);
        return $string;
    }
    add_filter('user_trailingslashit', 'nice_trailingslashit', 10, 2);

则还需要将该代码修改为:

    // 添加斜杠
    function nice_trailingslashit($string, $type_of_url) {
        if ( $type_of_url != 'single' && $type_of_url != 'page' )
          $string = trailingslashit($string);
        return $string;
    }
    add_filter('user_trailingslashit', 'nice_trailingslashit', 10, 2);

排除页面文件,否则页面链接.html后面也会自动加上斜杠。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
wordpress教程

WordPress 只允许游客浏览指定分类的文章新手教程

2020-12-23 12:21:27

wordpress教程

WordPress登陆和未登陆用户显示不同菜单

2020-12-25 17:24:03

!
你也想出现在这里?立即 联系我们吧!
信息
个人中心
购物车
优惠劵
今日签到
搜索