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();
}
相关推荐
benbenAItalk42 分钟前
数字人口播视频的批量生产实践:素材规范、任务编排与质量验收
java·前端·音视频
2601_962218611 小时前
万象生鲜系统业财一体化底层打通技术自动生成经营账单
大数据·数据库·人工智能·python·算法
2601_962885721 小时前
如何用 Python 扫描 A 股跳空缺口并统计缺口回补概率?
java·前端·python
西瓜太郎12342 小时前
Claude Code、Codex CLI、Gemini CLI 能否共用一枚 Key?先看协议选择矩阵
前端·api 网关·claude code·gemini cli·codex cli
Easy_API2 小时前
从“有多少卡“到“卖多少 Token“:算力的标尺正在换
大数据·人工智能·深度学习
移动云开发者联盟3 小时前
中国移动发布全链条算力服务成果
大数据
晓窗科技3 小时前
口碑好的AI基座服务商
大数据·人工智能·python
陈随易3 小时前
在Finch用了62亿词元,我认为这是新一代Agent工具之神
前端·人工智能·后端
和裕3 小时前
蜂窝板 vs 七层瓦楞重型纸箱:大件工业设备运输性能与成本全对比
大数据·运维·网络·人工智能·算法
ACP广源盛139246256734 小时前
国产 PCIe2.1 交换芯片 IX6024:低成本轻量化端侧 AI IO 扩展选型解析
大数据·人工智能·硬件架构·pcie·国产芯片