如何扩展此功能以检查当前自定义帖子类型是否属于后代类别

时间:2010-07-11 12:44:04

标签: php wordpress categories

我使用以下函数检查所查看的帖子是否属于给定ID的后代类别。

如何扩展该功能以检查当前查看的“自定义帖子类型”是否属于给定ID的后代类别?

我使用此功能将菜单项设置为“活动”。

功能是:

/**
  * Tests if any of a post's assigned categories are descendants of target categories
  *
  * @param int|array $cats The target categories. Integer ID or array of integer IDs
  * @param int|object $_post The post. Omit to test the current post in the Loop or main query
  * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
  * @see get_term_by() You can get a category by name or slug, then pass ID to this function
  * @uses get_term_children() Passes $cats
  * @uses in_category() Passes $_post (can be empty)
  * @version 2.7
  * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
  */
function post_is_in_descendant_category( $cats, $_post = null )
{
 foreach ( (array) $cats as $cat ) {
  // get_term_children() accepts integer ID only
  $descendants = get_term_children( (int) $cat, 'category');
  if ( $descendants && in_category( $descendants, $_post ) )
   return true;
 }
 return false;
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

这不适用于自定义帖子类型吗?你试过吗?