幸運的是,我剛有爲客戶的項目做到這一點。我用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/ –
我總是猶豫使用太多的插件,但我會定義嘗試一下!謝謝。 –
我完全同意你的看法,儘管最近我對真正基本的插件有點寬鬆,基本上只是從我們身上採取了一些咕嚕聲。希望能爲你解決問題! –