WordPress 自定义文章类型 固定链接 使用.html 后缀 图文教程

来源: 老季博客
日期: 2022-3-26
作者: 腾讯云/服务器VPS推荐评测/Vultr
阅读数: 44

当我们创建了自定义类型文章之后,默认的`固定链接`即使设置成 /%postname%.html ,自定义文章的后缀也不带.html ,所以我们记录一下通过代码的方式来实现。

比如我们这里使用案例的类型

/jiloc-case/postname //默认url路径

/jiloc-case/postname.html //修改之后的url路径

/**
 * 设置多种自定义文章类型的固定链接结构为 post_name.html
 */
$mytypes = array(//根据需要添加你的自定义文章类型
        'case' => 'jiloc-case',
	'product' => 'jiloc-product',
);
add_filter('post_type_link', 'my_custom_post_type_link', 1, 3);
function my_custom_post_type_link( $link, $post = 0 ){
    global $mytypes;
    if ( in_array( $post->post_type,array_keys($mytypes) ) ){
        return home_url( $mytypes[$post->post_type].'/' . $post->post_name .'.html' );
    } else {
        return $link; 
    }
}
add_action( 'init', 'my_custom_post_type_rewrites_init' );
function my_custom_post_type_rewrites_init(){
    global $mytypes;
    foreach( $mytypes as $k => $v ) {
		add_rewrite_rule($v.'/([^/]+)\.html$','index.php?post_type='.$k.'&name=$matches[1]','top');		
    }
} 
链接到文章: https://jiloc.com/47709.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注