幸运的是,我刚有为客户的项目做到这一点。我用this answer on the WordPress Stackexchange作为指导:
/**
* Tell WordPress how to interpret our project URL structure
*
* @param array $rules Existing rewrite rules
* @return array
*/
function so23698827_add_rewrite_rules($rules) {
$new = array();
$new['projects/([^/]+)/(.+)/?$'] = 'index.php?cpt_project=$matches[2]';
$new['projects/(.+)/?$'] = 'index.php?cpt_project_category=$matches[1]';
return array_merge($new, $rules); // Ensure our rules come first
}
add_filter('rewrite_rules_array', 'so23698827_add_rewrite_rules');
/**
* Handle the '%project_category%' URL placeholder
*
* @param str $link The link to the post
* @param WP_Post object $post The post object
* @return str
*/
function so23698827_filter_post_type_link($link, $post) {
if ($post->post_type == 'cpt_project') {
if ($cats = get_the_terms($post->ID, 'cpt_project_category')) {
$link = str_replace('%project_category%', current($cats)->slug, $link);
}
}
return $link;
}
add_filter('post_type_link', 'so23698827_filter_post_type_link', 10, 2);
在注册自定义后类型和分类,一定要使用以下设置:
// Used for registering cpt_project custom post type
$post_type_args = array(
'rewrite' => array(
'slug' => 'projects/%project_category%',
'with_front' => true
)
);
// Some of the args being passed to register_taxonomy() for 'cpt_project_category'
$taxonomy_args = array(
'rewrite' => array(
'slug' => 'projects',
'with_front' => true
)
);
当然,一定要刷新重写规则,当你”重做。祝你好运!
这个插件能解决你的问题吗? https://wordpress.org/plugins/custom-post-type-permalinks/ –
我总是犹豫使用太多的插件,但我会定义尝试一下!谢谢。 –
我完全同意你的看法,尽管最近我对真正基本的插件有点宽松,基本上只是从我们身上采取了一些咕噜声。希望能为你解决问题! –