在这篇文章中,我将分享如何在wordpress中重写自定义帖子类型的uri。这就是我用帖子id重写自定义帖子类型url的方法。您需要一个重写规则来翻译url请求,以及一个过滤器post_type_link以返回对以下任何调用的正确url?get_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’ );
}
如果您喜欢freewebmentor并希望做出贡献,则可以撰写文章并将其邮寄到freewebmentor@gmail.com。?您的文章将出现在freewebmentor主页上并为其他开发人员提供帮助。