Skip Navigation

[Resolved] Get the slug of the grandparent of the taxonomy archive

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

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 support ticket is created 6 years, 1 month 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)

Author
Posts
#1108864

I'm trying to dynamically show the slug of the 2nd-level term of a taxonomy archive.

My URL is hidden link

This is a taxonomy archive page. In the above example, I would like a way to print the text "knitting-superstar" to my layout.
We can get there by saying it's the 2nd level term in the class-part hierarchy, or that it's two levels ABOVE the current taxonomy term.

Christian already gave me Custom Code to create the shortcode [n-level-term-id]. So I can actually call the correct term ID by typing [n-level-term-id n='2'].

Here is the code he gave me:

function n_level_term_id_func($atts) {
  $a = shortcode_atts( array(
      'n' => '0'
  ), $atts );
  $id = 0;
  $current_taxonomy = isset(get_queried_object()->taxonomy) ? get_queried_object()->taxonomy : null;
  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;
  }
  return $id;
}
add_shortcode("n-level-term-id", "n_level_term_id_func");

Is there any way to do something extremely similar, but return the slug? (the words "knitting-superstar," in this case).
Thank you so much!

Liat Gat

#1109086

Hi, you can modify the shortcode a bit 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.

#1109569

Thank you so much Christian!

This worked exactly as I had hoped and it was super-easy. Now I can protect all my videos based on the n-level taxonomy term slug.

I hate to add one more question and I wish I knew enough about code to do it myself but I have to ask - is there a way to output the term name to this as well? Something like [n-level-term n="2" format="name"]?

So I could dynamically output the words "Knitting Superstar" to my layout? As in, "You have to buy the Knitting Superstar class to see this video."

Thank you so much and I promise to stop asking questions soon.

Thank you,
Liat

#1109724

Thanks Christian, forget my followup question. I'll try to protect the videos from the level of the video itself, maybe.
Thank you,
Liat