从多个自定义 post 类型正在使用的分类中检索自定义 post 类型的特定术语

Retrieve custom post type specific terms from a taxonomy that is being used by multiple custom post types

我有一个自定义分类法 (tax_classes),它已注册到两个自定义 post 类型(cpt_events 和 cpt_galleries)。在每个自定义 post 类型的 'index' 页面上,我想从该特定 CPT 的 tax_classes 分类法中获取术语。例如,在事件 CPT 上,我想显示其 posts post 仅使用的术语。

我调查了 get_terms('tax_classes');但这给了我分类法的所有术语。不幸的是,get_terms WP 函数没有 'post_type' 参数来帮助过滤。

如有任何帮助,我们将不胜感激

谢谢

J

你看过get_object_taxonomies(); ?

https://codex.wordpress.org/Function_Reference/get_object_taxonomies

它的第一个参数是自定义 post 类型,第二个参数是分类法。

<?php $desc = wp_get_object_terms( $post->ID, 'your-taxonomy' ); if ( ! empty( $desc ) ) { if ( ! is_wp_error( $desc ) ) { foreach( $desc as $term ) { echo $term->name; } } } ?>

$post->ID 知道您所在页面的自定义类型 post。上面的代码将查找 your-taxonomy 并将其回显。