admin管理员组文章数量:1025518
I have a custom taxonomy called activities and a custom role called operator. So, if example/operators/bird-watching is visited then I want to list all the operators that offers bird-watching. Bird-watching is a term of activities taxonomy. I have made a different template for this. I have also developed a url structure for this which is show below. All I want now is to load my custom template if someone visits example/operators/bird-watching
Tried this link but no help.
function resources_cpt_generating_rule($wp_rewrite) {
global $wp_rewrite;
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'activities',
'hide_empty' => false,
) );
$post_type = 'operator';
foreach ($terms as $term) {
$rules['operators/' . $term->slug . '/([^/]*)$'] = 'index.php?operators/'.$term->slug;
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'resources_cpt_generating_rule' );
I have a custom taxonomy called activities and a custom role called operator. So, if example/operators/bird-watching is visited then I want to list all the operators that offers bird-watching. Bird-watching is a term of activities taxonomy. I have made a different template for this. I have also developed a url structure for this which is show below. All I want now is to load my custom template if someone visits example/operators/bird-watching
Tried this link but no help.
function resources_cpt_generating_rule($wp_rewrite) {
global $wp_rewrite;
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'activities',
'hide_empty' => false,
) );
$post_type = 'operator';
foreach ($terms as $term) {
$rules['operators/' . $term->slug . '/([^/]*)$'] = 'index.php?operators/'.$term->slug;
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'resources_cpt_generating_rule' );
Share
Improve this question
asked Mar 22, 2019 at 5:04
saurav.roxsaurav.rox
2051 silver badge13 bronze badges
1 Answer
Reset to default 1In the link you attached, you have everything you need. You should:
- add query variable (e.g
operators_actv
), - add rewrite rule that sets this variable (
operators_actv
), - use the
template_include
action hook to load custom template if query variableoperators_actv
is set.
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;
}
Don't forget to refresh permalinks. Click Save in Dashboars -> Settings -> Permalinks
or do it from code with flush_rewrite_rules()
(how to recreate rewrite rules).
I have a custom taxonomy called activities and a custom role called operator. So, if example/operators/bird-watching is visited then I want to list all the operators that offers bird-watching. Bird-watching is a term of activities taxonomy. I have made a different template for this. I have also developed a url structure for this which is show below. All I want now is to load my custom template if someone visits example/operators/bird-watching
Tried this link but no help.
function resources_cpt_generating_rule($wp_rewrite) {
global $wp_rewrite;
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'activities',
'hide_empty' => false,
) );
$post_type = 'operator';
foreach ($terms as $term) {
$rules['operators/' . $term->slug . '/([^/]*)$'] = 'index.php?operators/'.$term->slug;
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'resources_cpt_generating_rule' );
I have a custom taxonomy called activities and a custom role called operator. So, if example/operators/bird-watching is visited then I want to list all the operators that offers bird-watching. Bird-watching is a term of activities taxonomy. I have made a different template for this. I have also developed a url structure for this which is show below. All I want now is to load my custom template if someone visits example/operators/bird-watching
Tried this link but no help.
function resources_cpt_generating_rule($wp_rewrite) {
global $wp_rewrite;
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'activities',
'hide_empty' => false,
) );
$post_type = 'operator';
foreach ($terms as $term) {
$rules['operators/' . $term->slug . '/([^/]*)$'] = 'index.php?operators/'.$term->slug;
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'resources_cpt_generating_rule' );
Share
Improve this question
asked Mar 22, 2019 at 5:04
saurav.roxsaurav.rox
2051 silver badge13 bronze badges
1 Answer
Reset to default 1In the link you attached, you have everything you need. You should:
- add query variable (e.g
operators_actv
), - add rewrite rule that sets this variable (
operators_actv
), - use the
template_include
action hook to load custom template if query variableoperators_actv
is set.
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;
}
Don't forget to refresh permalinks. Click Save in Dashboars -> Settings -> Permalinks
or do it from code with flush_rewrite_rules()
(how to recreate rewrite rules).
本文标签: pluginsCustom url structure for custom template
版权声明:本文标题:plugins - Custom url structure for custom template 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745629999a2160102.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论