WordPress 自定义文章类型、自定义分类、标签、页面的URL伪静态

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A


//WordPress 自定义分类、标签、页面的URL伪静态格式,加.html后缀
function custom_page_rules() {

global $wp_rewrite;

/** page页面自定义URL样式 **/

$wp_rewrite->page_structure = $wp_rewrite->root . 'page/%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']['struct'] = $wp_rewrite->extra_permastructs['category']['with_front'].'/%category%.html';

}
add_action( 'init', 'custom_page_rules' );

//WordPress 自定义文章类型video以id.html重写
add_filter('post_type_link', 'custom_video_link', 1, 3);
function custom_video_link( $link, $post = 0 ){
if ( $post->post_type == 'video' ){
return home_url( 'video/' . $post->ID .'.html' );
} else {
return $link;
}
}
add_action( 'init', 'video_rewrites_init' );

function video_rewrites_init(){
add_rewrite_rule(
'video/([0-9]+)?.html$',
'index.php?post_type=video&p=$matches[1]',
'top' );
add_rewrite_rule(
'video/([0-9]+)?.html/comment-page-([0-9]{1,})$',
'index.php?post_type=video&p=$matches[1]&cpage=$matches[2]',
'top'
);
}

2 thoughts on “WordPress 自定义文章类型、自定义分类、标签、页面的URL伪静态

Leave a Comment