I have a hierarchical taxonomy called ‘geographical locations’. It contains continents on a first level, and then the countries for each one. Example :
Europe
- Ireland
- Spain
- Sweden
Asia
- Laos
- Thailand
- Vietnam
etc.
Using get_terms() I managed to output the full list of terms, but the continents get mixed up with the countries, in one big flat list.
How can I output a hierarchical list like above?
Use
wp_list_categories
with the'taxonomy' => 'taxonomy'
argument, it’s built for creating hierarchical category lists but will also support using a custom taxonomy..Codex Example:
Display terms in a custom taxonomy
If the list comes back looking flat, it’s possible you just need a little CSS to add padding to the lists, so you can see their hierarchical structure.
I realize, this is a very old question, but if you have a need to build up an actual structure of terms, this might be a useful method for you:
Usage is as follows:
I dont know of any function that does what you want but you can build up something like this:
I haven’t tested this but you can see what I’m getting at. What the above code will do is give you only two levels
EDIT: ahh yes you can use wp_list_categories() to do what you after.
You can use wp_list_categories(), with a ‘taxonomy’ argument.
The following code will generate drop-down with terms, but also can generate any other element/structure by editing the $outputTemplate variable, and editing str_replace lines:
I used @popsi code that was working really well and I made it a more efficient and easy to read:
Usage :
As I was looking for the same but to get terms of one post, finally I compiled this, and it works for me.
What it does :
⢠it gets all terms of a taxonomy name for a specific post.
⢠for a hierachical taxonomy with two levels (ex: level1:’country’ and level2:’cities’), it creates a h4 with the level1 followed by an ul list of level2 and this for all level1 items.
⢠if the taxonomy is not hierarchical, it will create only an ul list of all items.
here is the code (I write it for me so I tried to be as generic as I can but…) :
So finally you call the function with this (obviously, you replace my_taxonomy by yours) :
finishingLister('my_taxonomy');
I don’t pretend it’s perfect but as I said it works for me.
I had this problem and none of the answers here worked for me, for one reason or another.
Here is my updated and working version.
And usage:
This solution is less efficient than @popsi’s code, since it makes a new query for every term, but it’s also easier to use in a template. If your website uses caching, you may, like me, not mind the slight database overhead.
You don’t need to prepare an array that’ll be recursively filled with terms. You just call it the same way you would call get_terms() (the non-deprecated form with only an array for an argument). It returns an array of
WP_Term
objects with an extra property calledchildren
.Usage is simple:
Be sure that
hierarchical=true
is passed to yourget_terms()
call.Note that
hierarchical=true
is the default, so really, just be sure that it hasn’t been overridden to befalse
.Here I have four level dropdown select list with hidden first item