Wordpress-wp_get_object_terms-多个分类法

问题描述 投票:0回答:1

我目前正在使用以下内容从分类法中获取条款:

 $taxonomy = 'books';
 $post_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids'));

 if ( empty($post_terms) )
 return false;

 $terms = wp_get_object_terms($post->ID, $taxonomy, array('hide_empty' => false) );

现在我需要将代码扩展为另外两个分类法“ cds”和“ magazines”。

我希望通过添加这样的数组来修复它:

 $taxonomy = array(
                'books', 
                'cds', 
                'magazines'
 );
 $post_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids'));

 if ( empty($post_terms) )
 return false;

 $terms = wp_get_object_terms($post->ID, $taxonomy, array('hide_empty' => false) );

但是实际上似乎不起作用。

我在哪里错了?

php wordpress syntax shortcode taxonomy
1个回答
0
投票

如果要从多个分类法中检索多个术语,则必须将所有分类法传递为如下数组。

$post_terms= get_terms(
          'taxonomy' => array(
                         'books',
                         'cds',
                         'magazines')
);

经过测试,效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.