我的wordpress站点有一个简单层次的分类-
我正在使用下面的if语句来捕捉该帖子是否在类别博客或任何子类别中--而且它是有效的--但如果不能只检查当前类别的父类(另外我可能希望稍后添加类别),我会觉得很愚蠢。
if ( is_category('blog') || in_category(array('role-shift', 'urod-the-last-show', 'news')) )我已经搜索并尝试了每一个建议--包括cat_is_ancestor_of --但是都没有效果。
请帮帮我!
罗伯特
发布于 2017-04-29 07:43:58
$categories = get_the_category(); //returns categories
$thiscat = $categories[0];
$parent_id = $thiscat->parent; //the parent id
$parent = get_category($parent_id) //this returns the parent category as an object
//use id or slug of category you are searching for
if( $parent_id == 1 || $thiscat->slug == 'blog' ){
//this is a child of blog or the blog page
}这应该能起作用。这将确定当前类别是否是博客页面的子类别。第一部分get_category返回当前类别。
然后可以从当前类别获取父id,并使用“get_the_category_by_ID”获取父类别对象。
然后,您可以检查您是否在父类别下。
https://stackoverflow.com/questions/43693015
复制相似问题