Skip Navigation

[Resolved] Display Taxonomy Terms Hierarchically

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

Problem:
How can we create a list of Taxonomy Terms that are respecting the hierarchy of the terms as set in the backend?

Solution:
You can create a Parent View displaying all Terms without a parent - then as many Views as you have hierarchical layers
So if you have 2 more child terms (maximally) for each parent you'd add 2 more Views, each time querying the Terms that have as parent Term the one set in the "View where this View is inserted"
Then, insert each view into each other, so the Parent View holds a shortcode calling the next child view and that view loop, in turn, has a shortcode to another term child view, and so on.

Then you can insert the Parent View in a page and will see all terms output in the hierarchically correct order.

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

Our next available supporter will start replying to tickets in about 0.78 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 20 replies, has 2 voices.

Last updated by Jim 4 years, 1 month ago.

Assisted by: Beda.

Author
Posts
#1510153

Jim

I think I found a solution already here:
https://toolset.com/forums/topic/taxonomy-order-2/

I have a similar problem but I cannot use short codes because I use a view to display the taxonomy.

Is there a way to make this work inside a view?

This is my view code:
<div class="postcat [wpv-post-taxonomy type="category" format="slug" separator=" |"]"><a href="[wpv-post-taxonomy type="category" format="url"]">[wpv-post-taxonomy type="category" format="name"]</a></div>

#1510161

What is the issue you face, or goal?
Since you mention you cannot use the solution provided by Minesh, the issue and goal must be different

Can you elaborate on what you have, and what you need, so I can help find the right solution?
Note that ShortCodes work anywhere in WordPress, so there should not be any shortcode that won't work in a View, of course, if the ShortCode relies on data that the View cannot give, then it won't work, but the ShortCode itself still would try to expand.
You'd in this case likely have to amend the code that populates your Custom ShortCode, if you use any.

Thanks!

#1510217

Jim

Hi Beda, thanx for getting back so quickly!
Maybe you should forget the other example...

My goal is to display the taxonomy with the parent taxonomy first.

I have my taxonomy set up like this:
- Blog
- Nieuws
- Projecten
— Bioscopen
— Kantoren
— MFA
— Overheid
— Retail – Horeca
— Scholen
— Sportaccommodaties
— Zorginstellingen
- Vacatures

The problem is that my view displays the taxonomy alphabetically and not in Parent - Child order;

So if a post is in the category 'Bioscopen' within 'Projecten' it should display like this:

Projecten, Bioscopen

But now it doesn't.
It displays alphabetically like this: Bioscopen, Projecten

Hope that helps?

#1510287

I assume you mean a View that displays Posts, and with the ShortCode, you add the terms of each post in the View loop?
Those can only be ordered either Ascending or Descending by name, not by any other order, this is correct.

However, you could use another view, instead of the ShortCode.
Well, actually one additional View each "level" of hierarchy you want to display.

1. Create a View where you list all Terms of that Taxonomy
2. Add a Query filter to that View like "Parent is: None". This ensures, only actual "parent" terms are shown that in turn have no parent themselves. Also add a Query filter for what term to query, like "Set by the current post". This ensures, only Terms are returned that belong to the current post (which will be the post in your current Post View you use).
3. Now create a second View and again query terms of that taxonomy, this time set the query filter like "Parent is the taxonomy selected by the parent Taxonomy View"
4. Insert this View (#3) to the View created in #1, in its loop, just below the ShortCode to display the term name of the first View.
5. Make sure your second view also includes a shortcode for the term title it displays.
6. If you need more levels, nest another view in the second view, and so on, each time setting the filter to "Parent is the taxonomy selected by the parent Taxonomy View"

That will allow to finally insert the View #1 to the View of Posts you use currently, in its loop, just after the Post Title ShortCode, for example.
It will then display the Parent, it's children terms, their eventual children and next, another parent with the same child-list if available, and so on, until all terms of that post are rendered.

Then it will do the same for the next post in the loop.
Note that such structures can become heavy if the content grows, it is suggested to use pagination in the case you are above a certain amount of posts (this will depend on the level of nesting and complexity of posts loaded, but I would suggest generally not to have more than 10 results per "page", in this case).

Does this help?

#1510827

Jim

Hi Beda,
thanks for your extensive reply and solution! Appreciate i.

Sounds like a lot of work and that it could work but before I dive into that... isn't there a more simple solution?

I use the Generatepress theme and that theme had the same problem as my view on Archive pages.
Then I installed the plugin "Category Order and Taxonomy Terms Order" and without any changes the problem was solved on those pages!
Unfortunately not for my views...

But it does make me wonder, because of that simple solution for the Generatepress Archive pages query, something similar must be possible for toolset queries.

Also I think that with multiple nested queries the complexity does not benefit the websites speed and performance, don't you think?

Hope to hear from you.

#1512857

Jim

Anyone?

#1514337

I'm sorry the delay, I am on weekend Sunday and Monday, please refer to https://toolset.com/forums/users/beda-s/

I just checked and actually, the Toolset ShortCode already respects the hierarchy.

So for example, if you have a post with Term Parent, Term Child and Term Second parent, Term Second Child, and I add these to the Post (or loop item template) like so
[wpv-post-taxonomy type="my-tax" order="desc"]. 

Then I see in the Front End the right hierarchy respected:
Term Parent, Term Child, Term Second parent, Term Second Child

So, this would be the solution applied directly to posts, or loop items, saving you the nesting of views.
If more control is needed on that, nesting of the Views should be considered.

#1514707

Jim

This is not working for me...
Are you sure the items are not alphabetically ordered?

If I add the oder 'asc' or 'decs' to the shortcode it changes the order alphabetically, not Parent-Child

Any idea?

#1516031

I'm sorry, you are right:
I fooled myself with a few alphabetically hierarchically ordered terms - and of course, it orders alphabetically, I made a bad test yesterday which a new test confirmed to be wrong.

So yes, it's only possible with the nested Views.
Or, well, a Custom Code ShortCode.

The best function to use would be https://developer.wordpress.org/reference/functions/wp_get_post_terms/, but this still won't give you the hierarchically ordered terms
It will nee more custom work, see this as an example https://wordpress.stackexchange.com/questions/172118/get-the-term-list-by-hierarchy-order

The way of doing it with Views is easier, in this aspect, but maybe a little more expensive in query time (although that will mostly depend on the amount of content)

#1519655

Jim

I am getting close Beda!

I created the parent and the child view.

Now the parent category displays nicely but the child category does not.

I think the child view needs an additional query filter; one that selects only the (child) categories that belong to the post from the original loop.

Can you help me out here?

#1520187

Right, the Child Views require a Query Filter, that grabs the Terms which have as a parent the term displayed in the parent View, or loop.
See #3 on https://toolset.com/forums/topic/display-taxonomy-in-order-primary-first/#post-1510287:
3. Now create a second View and again query terms of that taxonomy, this time set the query filter like "Parent is the taxonomy selected by the parent Taxonomy View"

Does this help?

#1520379

Jim

Yes I know Beda. But I mean a second filter because now it was collecting all terms under that parent category.
I found it already and now it works .

The last 'problem' is that I want to add a comma seperation between the parent and the child term, but there is a space before the comma (look at the source html, a strange large white space is before the comma):

Parent view

<wpv-loop><div class="postcat [wpv-taxonomy-slug]"><a href="[wpv-taxonomy-url]">[wpv-taxonomy-title]</a>[wpv-view name="child-categorie"]</div></wpv-loop>

Child view

<wpv-loop>, <a href="[wpv-taxonomy-url]">[wpv-taxonomy-title]</a></wpv-loop>

Output:
hidden link

html source:
hidden link

#1520381

Try to check the "Disable the wrapping DIV around the View" setting in the View, just below the Loop Editor.
That will remove this empty space, I think - it is usually due to the native core output of any Loop that is never fully "raw"

#1520395

Jim

That was already disabled...
Any other solutions?

#1520623

Jim

A solution could be to put the comma as a trailing character after the parent term, but then if the post does not have child categories the comma must be hidden...

I'm sure there must be a solution as this is something that happens al the time using views right?

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