wordpress 文章页,文章分类,单页,woocommerc 产品页,分类页添加.html后缀

php 复制代码
// 设置分类,页面,TAG HTML结尾
function custom_page_rules() {
global $wp_rewrite;
/** page页面自定义URL样式 **/
//$wp_rewrite->page_structure = $wp_rewrite->root . 'page/%pagename%.html';
//$wp_rewrite->page_structure = $wp_rewrite->root . '%pagename%.html';
/** tag页面自定义URL样式 **/
//$wp_rewrite->extra_permastructs['post_tag']['with_front'] = '';
//$wp_rewrite->extra_permastructs['post_tag']['struct'] = $wp_rewrite->extra_permastructs['post_tag']['with_front'] . 'tag/%post_tag%.html';
/** category页面自定义URL样式,这个打开文章详情页出问题 **/
//$wp_rewrite->extra_permastructs['category']['with_front'] = 'category';
//$wp_rewrite->extra_permastructs['category']['with_front'] = '';
//$wp_rewrite -> extra_permastructs['category']['struct'] = $wp_rewrite->extra_permastructs['category']['with_front'].'%category%.html';

$wp_rewrite->extra_permastructs['product_cat']['with_front'] = '';
//$wp_rewrite->extra_permastructs['product_cat']['ep_mask'] = '512';
$wp_rewrite -> extra_permastructs['product_cat']['struct'] = $wp_rewrite->extra_permastructs['product_cat']['with_front'].'Cate/%product_cat%.html';
$wp_rewrite->extra_permastructs['product']['with_front'] = '';
//$wp_rewrite->extra_permastructs['product']['ep_mask'] = '512';
$wp_rewrite -> extra_permastructs['product']['struct'] = $wp_rewrite->extra_permastructs['product']['with_front'].'product/%product%.html';
//print_r($wp_rewrite);exit();
}
add_action( 'init', 'custom_page_rules' );
function add_html_suffix($content) {
    return $content . '.html';
}
//add_filter('post_link', 'add_html_suffix', 10, 2);
add_filter('page_link', 'add_html_suffix', 10, 2);
#add_filter('product_link', 'add_html_suffix', 10, 2);

function custom_product_permalink_with_html($permalink, $post) {
    #if ($post->post_type == 'product') {
        return untrailingslashit($permalink) . '.html';
    #}
    #return $permalink;
}
//add_filter('post_type_link', 'custom_product_permalink_with_html', 10, 2);
// 仅针对分类目录 (category)
add_filter('term_link', function($termlink, $term, $taxonomy) {
    
    if ($taxonomy === 'category') {
        $termlink = untrailingslashit($termlink) . '.html';
    }
    return $termlink;
}, 10, 3);

以上代码添加到主题 function.php <? 后面,部分页面出现404,还需要在 /index.php中添加对应代码去掉 .html

php 复制代码
if(isset($_SERVER['REQUEST_URI'])){
    if($_SERVER['REQUEST_URI']=='/news-center.html' || $_SERVER['REQUEST_URI']=='/about-us.html' || $_SERVER['REQUEST_URI']=='/shop.html'  || $_SERVER['REQUEST_URI']=='/projects.html'  || $_SERVER['REQUEST_URI']=='/news-center.html'){
        $_SERVER['REQUEST_URI']=str_replace('.html', "", $_SERVER['REQUEST_URI']);
    }
    //echo $_SERVER['REQUEST_URI'];exit();
}
相关推荐
2601_962885726 分钟前
quotes 字段全解:涨跌幅、振幅、换手率的口径与单位
java·前端·spring
key_3_feng28 分钟前
反馈闭环:Agent 自我进化的真正引擎
大数据·agent
excel28 分钟前
encodeURI 和 encodeURIComponent 有什么区别?
前端
前端精髓31 分钟前
调用openrouter的免费API模型
java·前端·javascript
EnCi Zheng1 小时前
Agent 如何进行工具调用:从一次 Function Calling 到生产级工具编排的完整链路
大数据·人工智能·agent·ai编程·工具调用
赛博守夜人1 小时前
AI安全攻防与治理之十四:AI 安全测试不能脱离传统安全,Web、API、移动端与模型攻击面的组合拳
前端·人工智能·安全
开开心心_Every1 小时前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea
GlobalInfo1 小时前
重磅发布 | 2026年船体清洁机器人行业研究报告:市场规模、发展前景与十五五战略地图
大数据·人工智能·机器人
海宇服务1 小时前
零信任架构实战:基于海宇全能小微企业报告构建自动化园区商户免押租赁资信核验网关
大数据·人工智能·架构·自动化
networknomad1 小时前
OpenReplay:自己搭的会话回放工具,给前端排错用的
前端·其他