在您附加的链接中,您拥有所需的一切。您应该:
添加查询变量(例如operators_actv
),添加设置此变量的重写规则(operators_actv
),使用template_include
用于加载自定义模板if查询变量的操作挂钩operators_actv
已设置Code:
add_filter( \'query_vars\', \'se332319_custom_query_vars\' );
add_filter( \'template_include\', \'se332319_custom_template\', 50 );
add_action( \'generate_rewrite_rules\', \'se332319_resources_cpt_generating_rule\' );
function se332319_resources_cpt_generating_rule( $wp_rewrite )
{
$rules = [
\'operators/([^/]+)/?$\' => \'index.php?operators_actv=$matches[1]&activities=$matches[1]\'
];
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
return $wp_rewrite;
}
function se332319_custom_query_vars( $query_vars )
{
array_push( $query_vars, \'operators_actv\' );
return $query_vars;
}
function se332319_custom_template( $template )
{
$qv = get_query_var(\'operators_actv\', null);
if ( $qv !== null && term_exists($qv, \'activites\') !== null ) {
// use "get_stylesheet_directory()" or "get_template_directory()"
// if template file is in theme directory
$template = dirname(__FILE__) . \'/se332319_custom_template.php\';
}
return $template;
}
别忘了刷新永久链接。点击
Save 在里面
Dashboars -> Settings -> Permalinks
或者从代码中使用
flush_rewrite_rules()
(
how to recreate rewrite rules).