是否可以自动重定向child taxonomy
至itsparent
, 因此,对于child taxonomy
?
Example:
我有一个分类法叫做
venue
, 此设置为
\'hierarchical\' => true,
. 这使我可以添加为赞助目的而重命名的场馆(例如:城市剧院成为XZY城市剧院,然后是ABC城市剧院,它们都是相同的建筑/位置,只是名称不同)。
因此,如果您单击child taxonomy
(XZY城市剧院)你会被带到https://domain.com/venue/city-theatre
不是https://domain.com/venue/xyz-city-theatre
.
Venue Taxonomy
这是我创建分类法本身的代码
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
这是分类页面本身的代码。
父级显示分配给父级和子级的所有帖子(这是我希望为所有人显示的内容)。
$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 }
如果无法重定向,如何显示
parent
和
sibling
所有上的帖子
child
页码?可能会改变
canonical link
在子页面上指向
parent
页
Edit: Redirect works with
根据CodeFinger先生的答案进行工作。将以下内容添加到“分类场所”的顶部。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\' );
}