Skip Navigation

[Resolved] Create a custom Layout just for one specific level of hierarchy in a taxonomy

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

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 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 5 replies, has 3 voices.

Last updated by liatG 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1096412

Hi there, this is for Christian Cox please.

Hi Christian, could you please provide me a custom code snippet that will allow me to apply a separate layout for a specific level of hierarchy in a taxonomy as we mentioned in https://toolset.com/forums/topic/help-using-custom-taxonomies-to-create-online-tutorials-and-their-navigation/#post-1096407?

Let me know if you need anything from me in order to be able to do this.

Thank you!

#1096893

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Liat

Christian doesn't work today, his next day is Sunday.

Reply to this whether you need someone else to look into this or would like to wait for Christian.

#1096935

Hi Nigel,
I'd be willing to have anyone on the support team answer this if they can.
Thank you!
Liat

#1097139

Hi again,
I'd like to wait for Christian.
Thanks,
Liat

#1099582

Hi Liat, thanks for being patient. Here is a code snippet that will help you assign a different Layout to each level of terms in a hierarchical taxonomy. Add this code to your child theme's functions.php file:

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);
}

Change your-custom-taxonomy to be the slug of your custom taxonomy. If you want to use this same code for more than one taxonomy, add its slug here as well, separated by a comma.
Change 123, 456, 456, 789 to be a comma-separated list of Layout IDs. The first Layout ID will be used for the highest level taxonomy terms, then the next Layout ID for the next level taxonomy terms, and so on.

#1103731

Christian, this worked perfectly! Thank you so much.