WordPress 页面链接添加html后缀

WordPress 页面链接添加html后缀

作者 : 资源客 发布时间: 2019-11-27

默认 wordpress 页面不能实现伪静态链接,比如:http://www.qq301.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 后面也会自动加上斜杠。

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » WordPress 页面链接添加html后缀