腾讯云服务器优惠活动华纳云香港服务器

WordPress如何判定某篇自定义类型文章是否属于某个类型?

WordPress主题推荐

我们都知道想要判断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;
?>

本文地址:https://boke112.com/post/9535.html

赞 (0) 打赏
版权声明:本文为原创文章,版权归 boke112百科 所有,欢迎分享本文,转载请保留出处!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
wu