我们都知道想要判断WordPress的某篇普通文章类型post是否属于某个分类,只需要使用in_category()函数即可,那么我们自己创建的自定义类型文章如何判断它是否属于某个自定义分类呢?这个时候需要用到is_object_in_term()函数,下面boke112联盟就跟大家说一说这个is_object_in_term()函数的用法。
is_object_in_term()函数
- is_object_in_term( int $object_id, string $taxonomy, int|string|int[]|string[] $terms = null )
参数:
- $object_id:(int)(必需) 对象的 ID(帖子 ID、链接 ID、…)。
- $taxonomy:( string ) (必填) 单一分类法名称。
- $terms:( int | string | int[] | string[] ) (可选) 要检查的术语 ID、名称、slug 或数组,默认值:空。
举例:
- <?php
- if (is_object_in_term( $post->ID, 'custom_taxonomy_name', 'term_name' ) ) :
- echo 'YES';
- else :
- echo 'NO';
- endif;
- ?>
比如我想要判断某篇自定义类型文章是否属于自定义分类postwd中的分类WordPress(分类别名为:wordpress,分类ID为:99),则可以写成:
- <?php
- if (is_object_in_term( $post->ID, 'postwd', 'wordpress' ) ) :
- echo 'YES';
- else :
- echo 'NO';
- endif;
- ?>
或
- <?php
- if (is_object_in_term( $post->ID, 'postwd', '99' ) ) :
- echo 'YES';
- else :
- echo 'NO';
- endif;
- ?>
© 版权声明
THE END