将所有子分类重定向到其父分类

时间:2021-02-04 作者:robwatson_dev

是否可以自动重定向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 }
如果无法重定向,如何显示parentsibling 所有上的帖子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\' );
    } 

1 个回复
最合适的回答,由SO网友:mrcodefinger 整理而成

您可以检查$术语是否有父项,然后使用重定向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);
    } 
?>
我希望这有帮助!

格里茨

相关推荐

Filter Custom Taxonomy Posts

我有一个自定义的分类页面,它也有一个向上的滑块。问题是滑块显示来自所有分类法而非当前分类法的随机帖子。是否有办法过滤滑块中的图像,使其仅使用当前分类法?<?php global $taxonomy_location_url, $taxonomy_profile_url; $args = array( \'post_type\' => $taxonomy_profile_url, \'orderby\' => \'rand\', \'meta_que