Skip Navigation

[Resolved] how to create different Taxonomy archive page for parent & child taxonomy terms

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display archives differently for terms at different levels of hierarchy. For top-level terms, I would like to display archive style A, for second-level (child) terms archive style B, for third-level (grandchild) terms archive style C.

Solution: I think it would be best to set up multiple WordPress Archives. In the "Loops selection" section of these WP Archives, do not select anything. In the Loop Editor of each WordPress Archive, you will create one of the 3 different designs. The first one can be for the parent terms, and it will not include anything in the wpv-loop tags, because you do not want to display posts.

Create a View of Product Category Taxonomy Terms, filtered by term parent, where the term parent is set by the current archive. In the Loop Output section of this View, insert a link to the term archive page. Then insert this View in the WordPress Archive you just created, somewhere outside the wpv-loop tags.

Create another WordPress Archive, and insert the same View of terms outside the wpv-loop tags. Build the product loop inside the wpv-loop tags. This WP Archive will be used to display the child term archive page.

Create a third WordPress Archive, and build the product loop inside the wpv-loop tags.

We offer a filter called wpv_filter_force_wordpress_archive that will allow you to apply a different WordPress Archive to the current archive page, based on some criteria. I have a code snippet that can help. Add this to your child theme's functions.php file:

// 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;
  // only for product category taxonomy
  if( !$current_taxonomy || $current_taxonomy != 'product_cat' )
    return $wpa_to_apply;
 
  $current_term_level = get_tax_level(get_queried_object()->term_id, $current_taxonomy);
  if ($current_term_level == 1) {
    // show top-level archive
    $wpa_to_apply = 123;
  } else if ($current_term_level == 2) {
    // show mid-level archive
    $wpa_to_apply = 234;
  } else {
    // show third-level archive
    $wpa_to_apply = 345;
  }
  return $wpa_to_apply;
}

You will modify 123, 234, and 345 to match the numeric ID of the 3 WordPress Archives you want to apply to the 3 different levels of hierarchy. Level 1 corresponds to the parent term, level 2 corresponds to the child term, and level 3 corresponds to the grandchild term.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_wordpress_archive

This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by Tcoffee 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#950243
Screenshot_2.png
sub-child-taxomy.png
child-taxonomy.-page.png
parent-taxonomy-page.png

Tell us what you are trying to do? I'm trying to create a different archive page for parent & child taxonomy terms.

In the parent taxonomy archive page, I want to display the child taxonomy terms of the current taxonomy archive page.

In the child taxonomy archive page, I want to display the sub-child taxonomy terms of current taxonomy child term archive page.

In the sub child taxonomy term archive page, I want to display the products which come under this sub child taxonomy.

Currently, I'm able to achieve this partially (hidden link) where both product & child terms are getting displayed at the same time. in the parent taxonomy page, I want to show only child terms, not both.
How can I achieve this, please help.
Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site? hidden link

#950535

Hi, this would be difficult to set up in a single WordPress Archive, so I think it would be best to set up multiple WordPress Archives. In the "Loops selection" section of these WP Archives, do not select anything. In the Loop Editor of each WordPress Archive, you will create one of the 3 different designs. The first one can be for the parent terms, and it will not include anything in the wpv-loop tags, because you do not want to display posts.

Create a View of Product Category Taxonomy Terms, filtered by term parent, where the term parent is set by the current archive. In the Loop Output section of this View, insert a link to the term archive page. Then insert this View in the WordPress Archive you just created, somewhere outside the wpv-loop tags.

Create another WordPress Archive, and insert the same View of terms outside the wpv-loop tags. Build the product loop inside the wpv-loop tags. This WP Archive will be used to display the child term archive page.

Create a third WordPress Archive, and build the product loop inside the wpv-loop tags.

We offer a filter called wpv_filter_force_wordpress_archive that will allow you to apply a different WordPress Archive to the current archive page, based on some criteria. I have a code snippet that can help. Add this to your child theme's functions.php file:

// 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;
  // only for product category taxonomy
  if( !$current_taxonomy || $current_taxonomy != 'product_cat' )
    return;

  $current_term_level = get_tax_level(get_queried_object()->term_id, $current_taxonomy);
  if ($current_term_level == 1) {
    // show top-level archive
    $wpa_to_apply = 123;
  } else if ($current_term_level == 2) {
    // show mid-level archive
    $wpa_to_apply = 234;
  } else {
    // show third-level archive
    $wpa_to_apply = 345;
  }
  return $wpa_to_apply;
}

You will modify 123, 234, and 345 to match the numeric ID of the 3 WordPress Archives you want to apply to the 3 different levels of hierarchy. Level 1 corresponds to the parent term, level 2 corresponds to the child term, and level 3 corresponds to the subchild term. Let me know if you have questions about this approach, or if you already started another way and need help with that other method.

Edit - I made a small adjustment after publishing the first time that will prevent php notices on non-archive pages.

#951237

Hi,

thanks for the help, it was easy. but the solution is still incomplete.

Now I'm getting both products & sub child terms in the second archive page (link: hidden link) (child term).

I want to show only one ( if child term has children (sub-child) then show only sub child terms else show only products).

How can I achieve this, please help.

#951515

You can use another View to determine whether or not the current term has any descendent terms.
- Create a View of terms, filtered by term parent, where the term parent is set by the current archive.
- In the wpv-items-found tags, place the View of child term links
- In the wpv-no-items-found tags, place the View of product links
- Remove all code from between the wpv-loop tags, since this View is only needed to determine if any results exist.

#952287
1-min.jpg
2-min.jpg

Hi,
thanks a lot, that solved my issue.
I need a bit more help on 2 things
1. I want to show similar products on a child term archive & sub child term archive pages ( similar products = products come under same parent taxonomy (eg: in the product page, first it should list the products of the current child term and list products of remaining child terms of the same parent below that. see screenshot1)
2. I want to set up a sidebar in child term archive & sub child term archive pages, where the sidebar should only display the parent, child & siblings of the current archive page term. (see screenshot 2)

please help me with this, it is very important.

#953147

Our support policy is to limit each ticket to one issue, so I have created a couple of new tickets where we can address your new questions.

#953524

thanks, Christian. the new split question option is awesome,.