我很难让我的自定义帖子类型URL正常工作。我找到的每一个解决方案都削弱了另一个需要工作的部分。我要做的是domain.com/post_type/post_id/post_name
.
除了档案馆之外,我所有的东西都适用于:
register_post_type(\'post-type\',
array(
\'rewrite\' => array(
\'slug\' => \'post-type/%post_id%\',
\'with_front\' => false,
\'pages\' => true,
\'ep_mask\' => 1
)
))
那么我有:add_filter(\'post_type_link\', \'custom_post_type_link\', 1, 3);
function custom_post_type_link($post_link, $post = 0, $leavename = false) {
if ($post->post_type == \'post-type\')) {
return str_replace(\'%post_id%\', $post->ID, $post_link);
} else {
return $post_link;
}
}
所以我在想办法domain.com/post_type
工作。它目前抛出一个404。我知道如果我拆下过滤器/%post_id%
从我的重写中可以看出,存档将起作用。在此基础上,我尝试添加重写规则:
add_action( \'init\', \'custom_rewrites_init\' );
function custom_rewrites_init(){
add_rewrite_rule(
\'post-type/([0-9]+)?$\',
\'index.php?post_type=post-type&p=$matches[1]\',
\'top\' );
}
这样做,如果有人输入,URL不会重定向domain.com/post_type/post_id
和domain.com/post_type/post_id/post_name/2
不起作用。有人知道最好的方法吗?