If I want to display all terms assigned to a custom post with custom taxonomy, I could use for eg.
<?php the_terms( $post->ID, 'custom-taxonomy', '', ", ", '' ); ?>
but in this solution I need specify name of taxonomy.
I want to display all terms from all assigned to post taxonomies, but separetly. For eg. if I have taxonomies called "holidays
" and "countries
" I want to display their terms like that:
<div>
<span>[terms of "holidays"]</span>
<span>[terms of "countries"]<span>
</div>
How can I do that automatically, without giving the names of taxonomies?
Thanks!
If I want to display all terms assigned to a custom post with custom taxonomy, I could use for eg.
<?php the_terms( $post->ID, 'custom-taxonomy', '', ", ", '' ); ?>
but in this solution I need specify name of taxonomy.
I want to display all terms from all assigned to post taxonomies, but separetly. For eg. if I have taxonomies called "holidays
" and "countries
" I want to display their terms like that:
<div>
<span>[terms of "holidays"]</span>
<span>[terms of "countries"]<span>
</div>
How can I do that automatically, without giving the names of taxonomies?
Thanks!
Share Improve this question asked Jun 10, 2019 at 15:05 Piotr MileckiPiotr Milecki 253 bronze badges1 Answer
Reset to default 0You can get the taxonomies that are registered for a post type with get_object_taxonomies()
. If you pass the post object directly, you don't even need to explicitly name the post type either.
You can then loop through the result of that function to achieve the result you want:
$taxonomies = get_object_taxonomies( $post );
foreach ( $taxonomies as $taxonomy ) {
the_terms( $post->ID, $taxonomy, '<span>', ", ", '</span>' );
}