WordPress自定义文章类型URL的重写

下面以自定义类型 book 为例,把这段代码放入当前主题 functions.php 文件内,再去你的网站后台保存固定链接即可,
这样重写之后的url就变成了 www.ashuo.com/book/1.html
function customname_link($post_link, $post = 0) {
if($post->post_type === 'book') {
return home_url('book/' . $post->ID . '.html');
}
else{
return $post_link;
}
}
add_filter('post_type_link', 'customname_link', 1, 3);

function customname_rewrites_init(){
add_rewrite_rule('book/([0-9]+).html?$', 'index.php?post_type=book&p=$matches[1]', 'top');
}
add_action('init', 'customname_rewrites_init');