(更新/新答案)
因此,这里我将直接讨论您需要的代码:(但一定要阅读原始答案的更新版本)
注册CPT并设置正确的重写slug
register_post_type( \'english-speaking\', array(
\'label\' => \'English Speaking\',
\'public\' => true,
\'rewrite\' => array(
\'slug\' => \'english/speaking/%speaking_task%/%question_id%\',
),
// Other args here.
) );
这是针对“英语写作”CPT的,它不属于任何分类法:
register_post_type( \'english-writing\', array(
\'label\' => \'English Writing\',
\'public\' => true,
\'rewrite\' => array(
\'slug\' => \'english/writing/%question_id%\',
),
// Other args here.
) );
注册自定义分类法“口语任务”
register_taxonomy( \'speaking-task\', array( \'english-speaking\' ), array(
\'label\' => \'Speaking Tasks\',
\'public\' => true,
\'rewrite\' => array(
\'slug\' => \'english/speaking\',
),
// Other args here.
) );
确保重写
slug
设置为
english/speaking
.
“英语”档案可访问:
http://example.com/english/speaking
http://example.com/english/speaking/{SPEAKING TASK SLUG}
注册自定义重写标记add_rewrite_tag( \'%question_id%\', \'(\\d+)\' );
add_rewrite_tag( \'%speaking_task%\', \'([^/]+)\' );
添加替换自定义重写标记的代码add_filter( \'post_type_link\', \'my_filter_questions_post_type_link\', 10, 2 );
function my_filter_questions_post_type_link( $post_link, $post ) {
// Replaces/rewrites %question_id% in the permalink.
if ( false !== strpos( $post_link, \'%question_id%\' ) ) {
$id = get_post_meta( $post->ID, \'wpcf-question-id\', true );
// A default value is necessary, and the value has to be a 0.
$id = $id ? $id : \'0\';
$post_link = str_replace( \'%question_id%\', $id, $post_link );
}
// Replaces/rewrites %speaking_task% in the permalink.
if ( false !== strpos( $post_link, \'%speaking_task%\' ) ) {
// A default value is necessary, but the term/category doesn\'t need to
// actually exists. So you could, for example, use \'all\' as the value.
$slug = \'uncategorized\';
$cats = get_the_terms( $post, \'speaking-task\' );
if ( $cats && ! is_wp_error( $cats ) ) {
$slug = $cats[0]->slug;
}
$post_link = str_replace( \'%speaking_task%\', $slug, $post_link );
}
return $post_link;
}
刷新重写规则如果您不知道如何刷新规则,请参阅我的其他答案。。使用get_query_var()
检索重写标记值get_query_var( \'question_id\' )
对于%question_id%
get_query_var( \'task_slug\' )
对于%task_slug%
完整的示例代码is available here. :)如果使用插件注册post type, 您可以过滤(设置/更改)重写slug
通过register_post_type_args
过滤器,如下所示:
add_filter( \'register_post_type_args\', \'my_filter_post_type_args\', 10, 2 );
function my_filter_post_type_args( $args, $post_type ) {
if ( ! isset( $args[\'rewrite\'] ) ) {
$args[\'rewrite\'] = array();
}
if ( \'english-speaking\' === $post_type ) {
$args[\'rewrite\'][\'slug\'] = \'english/speaking/%speaking_task%/%question_id%\';
} elseif ( \'english-speaking\' === $post_type ) {
$args[\'rewrite\'][\'slug\'] = \'english/writing/%question_id%\';
}
return $args;
}
同样,如果您使用插件注册custom taxonomy, 您可以过滤(设置/更改)重写slug
通过register_taxonomy_args
过滤器,如下所示:add_filter( \'register_taxonomy_args\', \'my_filter_taxonomy_args\', 10, 2 );
function my_filter_taxonomy_args( $args, $taxonomy ) {
if ( ! isset( $args[\'rewrite\'] ) ) {
$args[\'rewrite\'] = array();
}
if ( \'speaking-task\' === $taxonomy ) {
$args[\'rewrite\'][\'slug\'] = \'english/speaking\';
}
return $args;
}
您可能需要设置较低的优先级;i、 e.更改10
到11
或更高的值(数字)—数字越大,优先级越低,而数字越低,优先级越高。或者,在没有自定义编码的情况下,如果插件允许您手动设置自定义帖子类型或分类的重写段塞,则只需使用适当的框/字段输入适当的重写段塞即可。=)