简体   繁体   English

从多个自定义帖子类型使用的分类中检索自定义帖子类型特定术语

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

I have a custom taxonomy (tax_classes) that has been registered to two custom post types (cpt_events and cpt_galleries). 我有一个自定义分类(tax_classes)已注册到两个自定义帖子类型(cpt_events和cpt_galleries)。 On the 'index' page for each of the custom post types I want to get the terms from the tax_classes taxonomy for that specific CPT. 在每个自定义帖子类型的“索引”页面上,我想从该特定CPT的tax_classes分类中获取术语。 For example on the events CPT I want to show the terms that have been used by its posts post only. 例如,在CPT事件中,我想要显示其帖子仅在帖子中使用的术语。

I have looked into get_terms('tax_classes'); 我查看了get_terms('tax_classes'); but this gives me ALL the terms for the taxonomy. 但这给了我分类法的所有条款。 Unfortunately there isn't a 'post_type' argument for the get_terms WP function to help with the filtering. 不幸的是,get_terms WP函数没有'post_type'参数来帮助进行过滤。

Any help on this would be greatly appreciated 任何有关这方面的帮助将不胜感激

Thanks 谢谢

J Ĵ

Have you looked at get_object_taxonomies(); 你看过get_object_taxonomies(); ?

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

Its first parameter is the custom post type and the second the taxonomy. 它的第一个参数是自定义帖子类型,第二个参数是分类法。

<?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 knows the custom-post-type of the page you are on. $post->ID知道您所在页面的自定义后期类型。 The above code will look for your-taxonomy and echo it out. 上面的代码将查找your-taxonomy并将其回显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM