Php 如何使用术语slug和POST ID这两个参数检查术语是否分配了POST?
我需要检查一篇文章是否分配了一个术语,但我没有关于该术语分类法的信息,因为它可能是我网站中存在的任何分类法。您可以使用自定义函数返回所有分类法的术语:Php 如何使用术语slug和POST ID这两个参数检查术语是否分配了POST?,php,wordpress,Php,Wordpress,我需要检查一篇文章是否分配了一个术语,但我没有关于该术语分类法的信息,因为它可能是我网站中存在的任何分类法。您可以使用自定义函数返回所有分类法的术语: /** * Get terms for all taxonomies * * @see get_object_taxonomies() */ function yourprefix_all_axonomies_terms($post_id) { $post = get_post( $post_id ); $po
/**
* Get terms for all taxonomies
*
* @see get_object_taxonomies()
*/
function yourprefix_all_axonomies_terms($post_id) {
$post = get_post( $post_id );
$post_type = $post->post_type;
$taxonomies = get_object_taxonomies( $post_type );
$out = [ ];
foreach ( $taxonomies as $taxonomy_slug ){
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$out[] = $term->slug;
}
}
}
return $out
}
然后像这样使用它:
if (in_array($term_looked_for, yourprefix_all_axonomies_terms( $post->ID ) ) {
// do your thing
}
还有一件事,如果我可以收集帖子ID和术语ID,那么可以不使用上面的任何函数直接检查它,或者如果我们知道术语ID和帖子ID,并且只需要查找帖子是否分配给该术语,我们如何进行检查。您可以修改上述函数并使用
wp\u get\u object\u terms
。(见文件)。不应该太难。