简体   繁体   English

如何仅为自定义帖子类型获取术语或分类法

[英]How to get terms or taxonomies only for custom post types

I have an array of post types and I want to fetch all categories or terms which only belong to these post types.我有一系列帖子类型,我想获取仅属于这些帖子类型的所有类别或术语。 Like product post type have a category with the name "product_cat" I used get_terms() but it returns all available terms.就像产品帖子类型有一个名为“product_cat”的类别,我使用get_terms()但它返回所有可用的术语。 If I use get_categories() It returns only terms of post type.如果我使用get_categories()它只返回帖子类型的条款。

$post_types = array('post', 'product', 'page');

You can to get it next - example of getting all custom taxonomy brand_categories of custom post brand您可以接下来获取它 - 获取自定义帖子brand的所有自定义分类brand_categories的示例

$args = array(
  'post_type' => 'brand',
  'taxonomy'  => 'brand_categories'
);
$categories = get_terms( $args );

Try with this code to get categories of post type product -尝试使用此代码获取帖子类型产品的类别 -

$args = array(
 'post_type' => 'product',
 'taxonomy'  => 'product_cat'
);
$categories = get_terms( $args );

For retrieving the categories need a taxonomy.检索类别需要分类法。 You must use the taxonomy without taxonomy can not get term details.您必须使用分类法,没有分类法无法获取术语详细信息。

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

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