archive – Use different archive for the same CPT or Taxonomy

Sometimes it could be useful to assign a different archive to terms of the same taxonomy or to apply different logics to assign and manage the archives.

Toolset offers the wpv_filter_force_wordpress_archive hook. This is an example how to use it:

/* ASSIGN ARCHIVE TEMPLATE TO TAXONOMIES AND TERMS */

// utility function to get the hierarchical level of some hierarchical taxonomy term
function get_tax_level($id, $tax){
  $ancestors = get_ancestors($id, $tax);
  return count($ancestors)+1;
}

// filter that switches wordpress archives for a specific taxonomy based on term hierarchy
add_filter( 'wpv_filter_force_wordpress_archive', 'switch_tax_archive_by_level', 30, 2 );
function switch_tax_archive_by_level( $wpa_assigned, $wpa_loop ) {

  $wpa_to_apply = $wpa_assigned;

  $current_taxonomy = isset(get_queried_object()->taxonomy) ? get_queried_object()->taxonomy : null;  

  if ( $wpa_loop == 'view_taxonomy_loop_product_cat' ) {

    $term_slug = get_queried_object()->slug;

    $current_term_level = get_tax_level(get_queried_object()->term_id, $current_taxonomy);

    if ($current_term_level == 1 || $term_slug == 'category1') {
      // show top-level archive
      $wpa_to_apply = 9408;
    } else {
      $wpa_to_apply = 4002;
    }
  }

  if ( $wpa_loop == 'view_taxonomy_loop_brand' ) {
    $wpa_to_apply = 4163;
  }
  if ( $wpa_loop == 'view_search-page' ) { 
    $wpa_to_apply = 5160;
  }
  if ( $wpa_loop == 'view_home-blog-page' ) {
    $wpa_to_apply = 7219;
  } 
  if ( $wpa_loop == 'view_taxonomy_loop_category' ) {
    $wpa_to_apply = 4527;
  }
  write_log('$wpa_to_apply: '.$wpa_to_apply);
  return $wpa_to_apply;
}


You can find this solution on this Toolset forum thread:

https://toolset.com/forums/topic/how-to-create-different-taxonomy-archive-page-for-parent-child-taxonomy-terms/