这就是我用来用帖子ID重写自定义帖子类型URL的方法。您需要一个重写规则来转换URL请求,以及post_type_link
为任何调用返回正确的URLget_post_permalink()
:
add_filter(\'post_type_link\', \'wpse33551_post_type_link\', 1, 3);
function wpse33551_post_type_link( $link, $post = 0 ){
if ( $post->post_type == \'product\' ){
return home_url( \'product/\' . $post->ID );
} else {
return $link;
}
}
add_action( \'init\', \'wpse33551_rewrites_init\' );
function wpse33551_rewrites_init(){
add_rewrite_rule(
\'product/([0-9]+)?$\',
\'index.php?post_type=product&p=$matches[1]\',
\'top\' );
}