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();
}
相关推荐
EQUINOX12 小时前
【论文精读】| CLIP精读
大数据·人工智能
weixin_382395233 小时前
为小工厂量身打造:本地部署的物料管理系统带缺料计算
前端·制造
__zRainy__3 小时前
解决pnpm v10+不自动构建
前端·pnpm·工程化
漏刻有时3 小时前
PHP GeoJSON转PNG地图渲染程序开发笔记、源码解读、问题复盘与整改方案
android·笔记·php
猫猫不是喵喵.4 小时前
Vue3 Props 属性
前端·javascript·vue.js
微三云 - 廖会灵 (私域系统开发)4 小时前
消费即资产:积分增值异业联盟商业模式完整拆解(附落地逻辑、合规方案与盈利模型)
大数据·人工智能
老白说数智化升级4 小时前
从一次改善到持续进化,AI如何沉淀制造现场的运营知识?
大数据·人工智能·制造
小也说互联网5 小时前
2026年|正规靠谱外贸独立站建站渠道全景解析
大数据·外贸独立站·独立站搭建·谷歌seo优化·外贸建站·谷歌建站服务商推荐·外贸出海企业必看
醉城夜风~5 小时前
CSS元素显示模式(display)
前端·css
只说证事5 小时前
电子商务专业考证能找到哪些工作
大数据