Problem: I would like to be able to get the slug or ID of a taxonomy term ancestor of the current taxonomy archive term.
Solution: Use a custom shortcode to return either an ID or a slug:
function n_level_term_func($atts) { $a = shortcode_atts( array( 'n' => '0', 'format'=>'id', ), $atts ); $current_taxonomy = get_queried_object()->taxonomy; $id = 0; $slug = ''; if( $current_taxonomy == 'class-part' ) { $ancestors = get_ancestors( get_queried_object()->term_id, $current_taxonomy ); $count = count( $ancestors ); $id = isset( $ancestors[ $count - $a['n'] ] ) ? $ancestors[ $count - $a['n']] : get_queried_object()->term_id; $term= get_term( $id, $current_taxonomy); $slug = isset($term->slug) ? $term->slug: $slug; } return $a['format']=='slug' ? $slug : $id; } add_shortcode("n-level-term", "n_level_term_func");
Then you can use the "format" attribute to specify if you want a "slug". Otherwise, an "id" will be returned:
Slug: [n-level-term n="2" format="slug"]<br /> ID: [n-level-term n="2"]<br />
You may have to re-register the new shortcode name in Toolset > Settings > Front-end content.
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 3 replies, has 2 voices.
Last updated by 6 years, 1 month ago.
Assisted by: Christian Cox.