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();
}
相关推荐
AcaDesign1 小时前
国家/省部市级科技奖答辩PPT_科技进步奖_自然科学奖_技术发明奖|WordinPPT
大数据·人工智能·科技·powerpoint
垂钓的小鱼11 小时前
用关联图谱做量化选股:让基本面惊喜沿着网络扩散
服务器·php·apache
Emily156853598702 小时前
Emerson 1C31129G03 Analog Input Module
java·开发语言·前端·plc·emerson·1c31129g03·input module
To_OC7 小时前
面试被问了三回三栏布局,这次我终于把 BFC 那层窗户纸捅破了
前端·css·面试
小淮AI7 小时前
国际教育课程的本土化探索:以枫叶教育三十年为观察样本
大数据·人工智能
To_OC7 小时前
啃完 TS 工具类型我发现:Pick 和 Omit 原来就是一层窗户纸
前端·面试·typescript
龙兵AI增长破局圈.赵老师讲成交8 小时前
只有把过程管好,结果才会出来。
大数据·人工智能·ai·创业创新
风月说与山鬼9 小时前
三、大括号语法
前端·react.js
qq407855609 小时前
面向智能补货需求的进销存系统盘点
大数据·人工智能·低代码·制造