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();
}
相关推荐
CodeSheep13 小时前
苦撑13年,创始人离职出走,拉勾终究还是倒下了…
前端·后端·程序员
ViiTor_AI13 小时前
行业洞察|跨境内容生产效率突围:重构出海团队的AI内容生产线
大数据·人工智能
a11177613 小时前
html制作的PPT(各种风格)提示词
前端·开源·html
cvcode_study13 小时前
Electron 制作自定义浏览器
前端·javascript·electron
JCJC错别字检测-田春峰13 小时前
字根秀秀 HTML 托管现已支持“用户登录”功能,一键变身 Web App!
前端·html·web app·网页托管
xixixi7777713 小时前
GPT-5.6(Iris-Alpha)细节泄露 + 国产 AI 芯片最高安全认证落地,全球 AI 格局迎来大变局
大数据·人工智能·gpt·ai·大模型·算力·智能体
胡楚昊13 小时前
在好靶场的WEB海洋遨游
前端
财经资讯数据_灵砚智能13 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年5月27日
大数据·人工智能·python·信息可视化·自然语言处理·ai编程·灵砚智能
天机️灵韵13 小时前
Tauri 2.0与Electron的桌面应用技术选型比较
前端·electron·前端框架