admin管理员组文章数量:1024603
I have custom post types for dining, lodging, and activities. Each of those custom post types uses a custom taxonomy called region. If, for example, I have a region called "downtown", I would like to be able to show dining option for that region by going to domain/downtown/dining. Or show hotels in that region by going to domain/downtown/lodging. So, basically, I like to create a rewrite rule to give the structure domain/tax-term/cpt-slug to show all the posts for the cpt that have that tax term selected. Any way to do this?
I have custom post types for dining, lodging, and activities. Each of those custom post types uses a custom taxonomy called region. If, for example, I have a region called "downtown", I would like to be able to show dining option for that region by going to domain/downtown/dining. Or show hotels in that region by going to domain/downtown/lodging. So, basically, I like to create a rewrite rule to give the structure domain/tax-term/cpt-slug to show all the posts for the cpt that have that tax term selected. Any way to do this?
Share Improve this question asked Apr 6, 2019 at 2:09 PediwentPediwent 436 bronze badges 2- 1 Looks like it's already answered here – Karun Commented Apr 6, 2019 at 2:47
- That's not exactly what I was looking for. In my case, my client doesn't want an overall cpt slug in the url. They want it to start with the slug of each cpt post. e.g. not /region/downtown/, but just /downtown/. In this case, the solution below seems to make more sense. – Pediwent Commented Apr 10, 2019 at 17:06
1 Answer
Reset to default 2Your permalink structure for taxonomy will conflict with default one for pages.
In the case of the address domain/item-one/item-two
how does Wordpress know if it's a page item-two
with the parent page item-one
or item-two
is custom post type and item-one
is term of region
taxonomy?
The order of rewrite rules will decide, and because rule for your structure will be first, links to child pages (like domain/parent-page/child-page
) will be directed to taxonomy page (taxonomy= "region", term= "parent-page", post_type= "child-page").
There are two ways, of which I would choose the first.
Add some prefix to your structure e.g.
regions/{term}/{cpt_slug}
Generate rewrite rules for all terms (
downtown
and others) fromregion
taxonomy (one rule for each term). Generating must be repeated after adding and removing the custom taxonomy term.
In both cases, to make rewrite rules work, you have to click Save in Dashboard -> Settings -> Permalinks
or use flush_rewrite_fules()
("important note")
Option #1
I used regions
as url prefix and region_cpt
as query variable name, you can change them to the ones you prefer.
add_action( 'init', 'se333633_rewrite_tags', 10 );
add_action( 'init', 'se333633_rewrite_rules' );
add_action( 'pre_get_posts', 'se333633_pre_get_posts', 30 );
function se333633_rewrite_tags()
{
add_rewrite_tag( '%region_cpt%', '([^/]+)' );
}
function se333633_rewrite_rules()
{
add_rewrite_rule(
'^regions/([^/]+)/([^/]+)/?$',
'index.php?taxonomy=region®ion=$matches[1]®ion_cpt=$matches[2]',
'top'
);
}
function se333633_pre_get_posts( $query )
{
$cpt_for_region = get_query_var( 'region_cpt', false );
if ($cpt_from_region !== false && $query->is_main_query() )
{
$query->query_vars['post_type'] = $cpt_for_region;
}
}
Option #2
In the code from option #1, se333633_rewrite_rules
changes and two new functions are added to refresh rewrite rules after changes in terms.
add_action( 'created_term', 'se333633_created_term', 20, 3);
add_action( 'delete_region', 'se333633_region_term_deleted' );
function se333633_rewrite_rules()
{
$region_terms = get_terms([
'taxonomy' => 'region',
'hide_empty' => false,
'fields' => 'id=>slug',
]);
if ( is_array($region_terms) && count($region_terms) )
{
foreach( $region_terms as $slug )
{
add_rewrite_rule(
'^'.$slug.'/([^/]+)/?$',
'index.php?taxonomy=region®ion='. $slug. '®ion_cpt=$matches[1]',
'top'
);
}
}
}
function se333633_created_term ( $term_id, $tt_id, $taxonomy )
{
if ( $taxonomy != 'region')
return;
flush_rewrite_rules();
}
function se333633_region_term_deleted( $term )
{
flush_rewrite_rules();
}
I have custom post types for dining, lodging, and activities. Each of those custom post types uses a custom taxonomy called region. If, for example, I have a region called "downtown", I would like to be able to show dining option for that region by going to domain/downtown/dining. Or show hotels in that region by going to domain/downtown/lodging. So, basically, I like to create a rewrite rule to give the structure domain/tax-term/cpt-slug to show all the posts for the cpt that have that tax term selected. Any way to do this?
I have custom post types for dining, lodging, and activities. Each of those custom post types uses a custom taxonomy called region. If, for example, I have a region called "downtown", I would like to be able to show dining option for that region by going to domain/downtown/dining. Or show hotels in that region by going to domain/downtown/lodging. So, basically, I like to create a rewrite rule to give the structure domain/tax-term/cpt-slug to show all the posts for the cpt that have that tax term selected. Any way to do this?
Share Improve this question asked Apr 6, 2019 at 2:09 PediwentPediwent 436 bronze badges 2- 1 Looks like it's already answered here – Karun Commented Apr 6, 2019 at 2:47
- That's not exactly what I was looking for. In my case, my client doesn't want an overall cpt slug in the url. They want it to start with the slug of each cpt post. e.g. not /region/downtown/, but just /downtown/. In this case, the solution below seems to make more sense. – Pediwent Commented Apr 10, 2019 at 17:06
1 Answer
Reset to default 2Your permalink structure for taxonomy will conflict with default one for pages.
In the case of the address domain/item-one/item-two
how does Wordpress know if it's a page item-two
with the parent page item-one
or item-two
is custom post type and item-one
is term of region
taxonomy?
The order of rewrite rules will decide, and because rule for your structure will be first, links to child pages (like domain/parent-page/child-page
) will be directed to taxonomy page (taxonomy= "region", term= "parent-page", post_type= "child-page").
There are two ways, of which I would choose the first.
Add some prefix to your structure e.g.
regions/{term}/{cpt_slug}
Generate rewrite rules for all terms (
downtown
and others) fromregion
taxonomy (one rule for each term). Generating must be repeated after adding and removing the custom taxonomy term.
In both cases, to make rewrite rules work, you have to click Save in Dashboard -> Settings -> Permalinks
or use flush_rewrite_fules()
("important note")
Option #1
I used regions
as url prefix and region_cpt
as query variable name, you can change them to the ones you prefer.
add_action( 'init', 'se333633_rewrite_tags', 10 );
add_action( 'init', 'se333633_rewrite_rules' );
add_action( 'pre_get_posts', 'se333633_pre_get_posts', 30 );
function se333633_rewrite_tags()
{
add_rewrite_tag( '%region_cpt%', '([^/]+)' );
}
function se333633_rewrite_rules()
{
add_rewrite_rule(
'^regions/([^/]+)/([^/]+)/?$',
'index.php?taxonomy=region®ion=$matches[1]®ion_cpt=$matches[2]',
'top'
);
}
function se333633_pre_get_posts( $query )
{
$cpt_for_region = get_query_var( 'region_cpt', false );
if ($cpt_from_region !== false && $query->is_main_query() )
{
$query->query_vars['post_type'] = $cpt_for_region;
}
}
Option #2
In the code from option #1, se333633_rewrite_rules
changes and two new functions are added to refresh rewrite rules after changes in terms.
add_action( 'created_term', 'se333633_created_term', 20, 3);
add_action( 'delete_region', 'se333633_region_term_deleted' );
function se333633_rewrite_rules()
{
$region_terms = get_terms([
'taxonomy' => 'region',
'hide_empty' => false,
'fields' => 'id=>slug',
]);
if ( is_array($region_terms) && count($region_terms) )
{
foreach( $region_terms as $slug )
{
add_rewrite_rule(
'^'.$slug.'/([^/]+)/?$',
'index.php?taxonomy=region®ion='. $slug. '®ion_cpt=$matches[1]',
'top'
);
}
}
}
function se333633_created_term ( $term_id, $tt_id, $taxonomy )
{
if ( $taxonomy != 'region')
return;
flush_rewrite_rules();
}
function se333633_region_term_deleted( $term )
{
flush_rewrite_rules();
}
本文标签: url rewritingTaxonomy rewrite question
版权声明:本文标题:url rewriting - Taxonomy rewrite question 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745620857a2159562.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论