Skip Navigation

[Resolved] Help creating a nested view of taxonomy terms to display on archive page sidebar

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

Problem: I would like to create a hierarchical menu of taxonomy term links.

Solution: Use multiple nested Views of this taxonomy. Filter these Views by term ID or term parent, and output the term links and/or another nested View in each Loop.
Use the following custom shortcode to get the ID of an ancestor term at a specific level of hierarchy:

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 == 'book-tax' ) {
    $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");

Register this shortcode in third-party shortcode arguments, then you can use it as a term ID filter for any View:

[wpv-view name="your-view-slug" terms="[n-level-term-id n='2']"]

Relevant Documentation:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

This support ticket is created 5 years, 7 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 19 replies, has 4 voices.

Last updated by liatG 5 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1096693
Sidebar - Online Class Chapter.png
Sidebar - Child Taxonomy Archive.png
Sidebar - Parent Taxonomy Archive.png
Sidebar - Hierarch Taxonomy Archive.png
View Loop - Terms Within Each Chapter.png
View Loop - Main Chapter Terms.png
View Setup - Terms Within Each Chapter.png
View Setup - Main Chapter Terms.png
Main Chapter Terms - Sidebar Widget.png

Good evening, this is a ticket for Christian Cox, please.

Hi Christian,
I attempted to set up the views for my sidebar navigation as you described in https://toolset.com/forums/topic/help-using-custom-taxonomies-to-create-online-tutorials-and-their-navigation/, but I don't think I did it exactly right.

I created a new sidebar and assigned it to Class Parts archive pages and Online Class Chapter (custom post type) pages. In it I put a WP Views widget for the View "Main Chapter Terms." See screenshot: Main Chapter Terms - Sidebar Widget.

I created two views like you described, one for Main Chapter Terms and one for Terms Within Each Chapter. See screenshots: View Setup - Main Chapter Terms and View Setup - Terms Within Each Chapter.

The loop for Main Chapter Terms contains the View for Terms Within Each Chapter. The loop for Terms Within Each Chapter contains just the taxonomy link. See screenshots: View Loop - Main Chapter Terms and View Loop - Terms Within Each Chapter.

Needless to say, I didn't quite get the desired result. See screenshots: Sidebar - Hierarch Taxonomy Archive, Sidebar - Parent Taxonomy Archive, Sidebar - Child Taxonomy Archive, and Sidebar - Online Class Chapter.

Can you look at my setup screenshots and tell me what I'm doing wrong?

Also can you tell me how to set up the HTML so that the terms come out as a nice-looking list instead of all run together in one long line like in the screenshots? Preferably the child terms would be indented a bit.

Thank you!
Liat

#1096895

Nigel
Supporter

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

Timezone: Europe/London (GMT+01: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.

#1096938

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

#1097026

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Liat,

Thank you for contacting our support forum.

Let me see if I understand correctly here. You want pretty much a nested view With the Parents on a different level than that of the children correct?

If so then you ca do this by following the instructions in the link below.
https://toolset.com/documentation/user-guides/using-a-child-view-in-a-taxonomy-view-layout/#taxonomy-view-within-taxonomy-view

Thanks,
Shane

#1097136

Thank you Shane.

I reviewed the post you sent. This was my understanding and that's what I set up.

I would like to have Christian take a look at this when he comes back, so that he can look at the screenshots I uploaded and tell me where I messed up. He also is familiar with my project from our video call.

Thank you,
Liat

#1099585

Hi, in the Loop item for Main Chapter Terms, I can see you have inserted the nested View but it looks like you have not output the Main Chapter Term itself. So in the Loop editor of the Main Chapter Terms View, you should insert a link to the taxonomy term just before the Content Template like this:

<li>[wpv-taxonomy-link][wpv-post-body view_template="Loop item in Main Chapter Terms"]</li>

That should fix the problem where the Main Chapter Terms are not appearing.

Next, in the Terms Within Each Chapter View, it looks like this View's Loop was created using the "unformatted" option. If you want a formatted list output style, place your cursor in the Loop Editor and then click the Loop Wizard button to re-run the Loop Wizard. Select the "Unordered List" option to create the markup structure for an unordered list.

Let's take a look at the results of these changes and go from there.

#1101762
Class Part Taxonomy Terms Hierarchy.png
Includes Sibling Terms.png
Loops Through More Items Than Expected.png
Working Sidebar Navigation.png

Hi Christian,

Thank you so much for your help on this and the other tickets too.
I followed your directions here and got the navigation working in a mostly expected way.

Disregarding the "no items found" under "Introduction" for now, here's a screenshot of what I accomplished which is basically what I wanted. See Screenshot: Working Sidebar Navigation. (YAAAY!)

My next questions are: as I scroll down past what I would expect to be the last Main Chapter Term ("Help and Resources" - does not contain any chapters for now), I see that the View includes more child terms as well - Ch. 1, 2, etc. As I continue scrolling down, I see that it includes Class Parts that are sibling terms of the "Knitting Superstar" term.

See Screenshots: Loops Through More Items Than Expected, Includes Sibling Terms, and Class Part Taxonomy Terms Hierarchy.

Just so you can see the Archive Page I'm on when I see this view in the sidebar, see Screenshot: Knitting Superstar Child Archive Page.

I want to see just the child terms of "Knitting Superstar" on this page, plus the terms within those chapters. Can you help me figure out what I need to change?

Thank you,
Liat

#1101791

Where are you testing this View on the site? Please provide the URL so I can have some context.

#1101824

Whoops, here you go: hidden link.

#1101835

I think I need to log in to see how you have this structured. Is that possible? If so, please provide login credentials in the private reply fields here.

#1101849

Okay this View "Main Chapter Terms" has a Query Filter by term and it is set by the parent View:
hidden link

However, there is no parent View, the View is displayed without being nested inside another View:
hidden link

You can modify the "Main Chapter Terms" View's Query Filter to specify the correct term parent filter by selecting one specific term like "Knitting Superstar". This option is not very flexible - the View can only be used on archives that are descendents of Knitting Superstar.

A more flexible approach is to create another View of this taxonomy that has a term filter set by a shortcode attribute, nest the current View inside that new View, then use the new View in a sidebar HTML widget where you can specify the shortcode attribute to set the parent term context. So the question becomes how to determine the correct "parent term" that should be used as a reference on each Class Part term archive page? Is it always the ancestor one level below "My Classes", i.e. Knitting Superstar or Brioche Knitting, or is the logic for determining the parent term different depending on some other criteria?

#1101850

Thank you so much for helping me think of a simpler, more flexible way!

Yes, the correct parent term would always be the ancestor one level below "My Classes", i.e. Knitting Superstar or Brioche Knitting.

😀

Liat

#1103667

You can use a custom shortcode to get the 2nd level term ancestor in a taxonomy archive. Add this code to functions.php:

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 == 'book-tax' ) {
    $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");

Create a new View of this Taxonomy filtered by term, set to "Terms with ID set by the shortcode attribute: terms". In the Loop of this View, insert your Main Chapter Terms View. Then replace the WP Views sidebar widget with an HTML widget. Insert your new View in the HTML widget and use the custom shortcode in the "terms" shortcode attribute, like this:

[wpv-view name="your-view-slug" terms="[n-level-term-id n='2']"]

Finally, go to Toolset > Settings > Frontend Content and scroll to the Third Party Shortcode Arguments section. Register n-level-term-id here.

#1103717
Can't Access Toolset Settings Page.png

Hi Christian,
Thank you so much.

I followed all your directions but when I clicked on Toolset > Settings, the page hangs and finally gives a 504 Gateway Timeout. I am able to access all the other Toolset pages like Layouts, Dashboard, etc. I tried this several times, and even removed the code you gave me from the functions.php file to see if that was the cause. It wasn't, so I put the code back.

Do you have any idea why I can't access the Settings page?

Thanks,
Liat

#1103744

Not sure, it timed out a few times for me as well. I was finally able to access it by adding a URL parameter like this:
hidden link
This could indicate some kind of caching issue, where the cache held on to a temporary error. If it continues, feel free to open another ticket. In the meantime, I added the n-level-term-id for you so you can continue.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.