I’ve created custom taxonomies on WordPress and I want to display the current post taxonomies on the post in a list.
I’m using the following code to display a custom taxonomy named “Job Discipline”:
<ul>
<?php $args = array('taxonomy' => 'job_discipline'); ?>
<?php $tax_menu_items = get_categories( $args );
foreach ( $tax_menu_items as $tax_menu_item ):?>
<li>
Job Discipline: <a href="<?php echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>">
<?php echo $tax_menu_item->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
That’s just one of many taxonomies I want to list.
The problem is that the above code is displaying all the “Job Disciplines” which have at least one post and not the current post taxonomy.
How can I sort out this issue?
How to display current post taxonomies and terms
Here is a modified code from the Codex (see link below) that will display all the taxonomies of the current post with attached terms:
This is used like this:
Demo output
The output might look like this if the current post has the taxonomies
country
andcity
:Reference
The original code example in the Codex:
http://codex.wordpress.org/Function_Reference/get_the_terms#Get_terms_for_all_custom_taxonomies
Hope this helps – I’m sure you can adapt this to your project 😉
Update
Here is one modification to achieve that:
Just in case someone wants to display them grouped by parent.
It’s basically the same answer as above. I used this answer form another post:
https://stackoverflow.com/a/12144671
to group them (by id and by parent).
Function modified to use it with objects:
Final function:
Used the same way:
Note: just working with one level children terms.