How to display all terms from all taxonomies in post, but separately for each taxonomy?

If I want to display all terms assigned to a custom post with custom taxonomy, I could use for eg.<?php the_terms( $p

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

You 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>' );
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745419271a4626893.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信