admin管理员组文章数量:1304187
Is it possible to automatically redirect a child taxonomy
to its parent
, therefore not having a public page at all for the child taxonomy
?
Example:
I have a taxonomy called venue
, this is set up with 'hierarchical' => true,
. This allows me to add venues which have been renamed for sponsorship purposes (eg: City Theatre becomes the XZY City Theatre, then ABC City Theatre, they are all the same building/location just different names).
Therefore if you clicked on a link for a child taxonomy
(XZY City Theatre) you'd be taken to NOT
.
Venue Taxonomy
This is my code for creating the taxonomy itself
add_action( 'init', 'gd_tax_venues', 0 );
function gd_tax_venues() {
$plural = 'Venues';
$single = 'Venue';
$slug = 'venue';
$labels = array(
'name' => $plural,
'singular_name' => $single,
'menu_name' => $plural,
'all_items' => 'All' . $plural,
'parent_item' => 'Parent ' . $single,
'parent_item_colon' => 'Parent ' . $single . ':',
'new_item_name' => 'New ' . $single,
'add_new_item' => 'Add New ' . $single,
'edit_item' => 'Edit ' . $single,
'update_item' => 'Update ' . $single,
'view_item' => 'View ' . $single,
'separate_items_with_commas' => 'Separate ' . $plural . ' with commas',
'add_or_remove_items' => 'Add or remove ' . $single,
'choose_from_most_used' => 'Choose from the most used',
'popular_items' => 'Popular ' . $single,
'search_items' => 'Search ' . $single,
'not_found' => 'Not found',
'no_terms' => 'No' . $single,
'items_list' => $single . ' list',
'items_list_navigation' => $single . ' list navigation',
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'meta_box_cb' => false,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'public' => true
);
register_taxonomy( $slug, array( 'gig' ), $args );
}
taxonomy-venue.php
This is the code for the taxonomy page itself.
The parent shows all the posts which are assigned to both the parent and children (This is what I want displayed for all).
$terms = wp_get_post_terms( $post->ID, 'venue' );
$terms_ids = [];
foreach ( $terms as $term ) {
$terms_ids[] = $term->term_id;
}
$args = array(
'post_type' => 'gig',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'venue',
'field' => 'term_id',
'terms' => $terms_ids
)
),
);
$query = new WP_Query($args);
if ( $query->have_posts() ) { ?>
<ul>
<?php
while ( $query->have_posts() ) {
$query->the_post();?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php } ?>
</ul>
<?php }
If the redirect isn't possible, how can I show the parent
and sibling
posts on all child
pages? and possibly change the canonical link
on child pages to point to the parent
page?
Edit: Redirect works with
Working based on the answer from mrcodefinger. Adding the following to the top of 'taxonomy-venue.php'
$term = get_queried_object();
$parent = ( isset( $term->parent ) ) ? get_term_by( 'id', $term->parent, 'venue' ) : false;
if( $parent ) {
$url = get_term_link($term->parent);
wp_redirect( $url, '301' );
}
Is it possible to automatically redirect a child taxonomy
to its parent
, therefore not having a public page at all for the child taxonomy
?
Example:
I have a taxonomy called venue
, this is set up with 'hierarchical' => true,
. This allows me to add venues which have been renamed for sponsorship purposes (eg: City Theatre becomes the XZY City Theatre, then ABC City Theatre, they are all the same building/location just different names).
Therefore if you clicked on a link for a child taxonomy
(XZY City Theatre) you'd be taken to https://domain/venue/city-theatre
NOT https://domain/venue/xyz-city-theatre
.
Venue Taxonomy
This is my code for creating the taxonomy itself
add_action( 'init', 'gd_tax_venues', 0 );
function gd_tax_venues() {
$plural = 'Venues';
$single = 'Venue';
$slug = 'venue';
$labels = array(
'name' => $plural,
'singular_name' => $single,
'menu_name' => $plural,
'all_items' => 'All' . $plural,
'parent_item' => 'Parent ' . $single,
'parent_item_colon' => 'Parent ' . $single . ':',
'new_item_name' => 'New ' . $single,
'add_new_item' => 'Add New ' . $single,
'edit_item' => 'Edit ' . $single,
'update_item' => 'Update ' . $single,
'view_item' => 'View ' . $single,
'separate_items_with_commas' => 'Separate ' . $plural . ' with commas',
'add_or_remove_items' => 'Add or remove ' . $single,
'choose_from_most_used' => 'Choose from the most used',
'popular_items' => 'Popular ' . $single,
'search_items' => 'Search ' . $single,
'not_found' => 'Not found',
'no_terms' => 'No' . $single,
'items_list' => $single . ' list',
'items_list_navigation' => $single . ' list navigation',
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'meta_box_cb' => false,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'public' => true
);
register_taxonomy( $slug, array( 'gig' ), $args );
}
taxonomy-venue.php
This is the code for the taxonomy page itself.
The parent shows all the posts which are assigned to both the parent and children (This is what I want displayed for all).
$terms = wp_get_post_terms( $post->ID, 'venue' );
$terms_ids = [];
foreach ( $terms as $term ) {
$terms_ids[] = $term->term_id;
}
$args = array(
'post_type' => 'gig',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'venue',
'field' => 'term_id',
'terms' => $terms_ids
)
),
);
$query = new WP_Query($args);
if ( $query->have_posts() ) { ?>
<ul>
<?php
while ( $query->have_posts() ) {
$query->the_post();?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php } ?>
</ul>
<?php }
If the redirect isn't possible, how can I show the parent
and sibling
posts on all child
pages? and possibly change the canonical link
on child pages to point to the parent
page?
Edit: Redirect works with
Working based on the answer from mrcodefinger. Adding the following to the top of 'taxonomy-venue.php'
$term = get_queried_object();
$parent = ( isset( $term->parent ) ) ? get_term_by( 'id', $term->parent, 'venue' ) : false;
if( $parent ) {
$url = get_term_link($term->parent);
wp_redirect( $url, '301' );
}
Share
Improve this question
edited Feb 4, 2021 at 11:37
robwatson_dev
asked Feb 4, 2021 at 10:07
robwatson_devrobwatson_dev
1107 bronze badges
1
- taxonomies don't have parent/child relationships, are you sure you didn't mean parent terms? – Tom J Nowell ♦ Commented Feb 4, 2021 at 10:47
1 Answer
Reset to default 1You can check if your $term have parent and then redirect using wp_redirect()
<?php
$term = get_queried_object();
$parent = ( isset( $term->parent ) ) ? get_term_by( 'id', $term->parent, 'types' ) : false;
if( $parent ) {
wp_redirect($term->parent);
}
?>
I hope this helps!
Greetz
本文标签: Redirect all child taxonomy to its parent
版权声明:本文标题:Redirect all child taxonomy to its parent 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741776096a2397035.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论