Problem: I would like to apply a different Layout to my custom taxonomy archives depending on the hierarchical level of the term.
Solution: Use custom code to programmatically select a specific Layout for each hierarchical level:
function apply_layout_by_archive_term_hierarchy( $id, $layout ){ // all the taxonomy slugs that should use these hierarchical layouts $taxonomies = array( 'your-custom-taxonomy' ); // the layout IDs for each level of hierarchy, separated by a comma $layouts = array( 123, 456, 456, 789 ); // --------------------------------------------------- // you should not edit anything below // --------------------------------------------------- $layout_to_apply = $id; $current_taxonomy = isset(get_queried_object()->taxonomy) ? get_queried_object()->taxonomy : null; // test for the correct taxonomy archive if( !$current_taxonomy || !in_array( $current_taxonomy, $taxonomies) ) return $layout_to_apply; // find the hierarchical term level and apply the corresponding layout $current_term_level = get_tax_level(get_queried_object()->term_id, $current_taxonomy); if( isset( $layouts[$current_term_level])){ return $layouts[$current_term_level]; } // fallback to original layout return $layout_to_apply; } add_filter('get_layout_id_for_render', 'apply_layout_by_archive_term_hierarchy', 10, 2); function get_tax_level($id, $tax){ $ancestors = get_ancestors($id, $tax); return count($ancestors); }
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 5 replies, has 3 voices.
Last updated by 6 years, 4 months ago.
Assisted by: Christian Cox.