最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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>' );
}
发布评论

评论列表(0)

  1. 暂无评论