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 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 6 years, 3 months ago.
Assisted by: Christian Cox.