• Is there a conditional function to check whether a post is *under* a certain category, even if it wasn’t exactly assigned to the category (eg, I have a ‘News’ category, I check the ‘Music’ subcategory under that, now in_category($newsid) returns false).

    Or do we need to use get_the_category and do a for loop through each category and a while loop all the way up its parents and parents’ parents?

Viewing 2 replies - 1 through 2 (of 2 total)
  • function in_cat_hierarchy($cat, $postid = '')
    {
    global $category_cache, $cache_categories, $post;

    if('' == $postid) $postid = $post->ID;
    $incats = array();

    update_post_category_cache($postid);
    $categories = $category_cache[$postid];
    foreach($categories as $category) {
    $incats[$category->cat_ID] = true;
    $incats[$category->category_nicename] = true;
    $parent = $category->category_parent;
    while($parent != 0) {
    $incats[$parent] = true;
    $incats[$cache_categories[$parent]->category_nicename] = true;
    $parent = $cache_categories[$parent]->category_parent;
    }
    }
    return !empty($incats[$cat]);
    }

    any explanation about what that means or where to put it? I need the same thing.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Check whether a post is under a category?’ is closed to new replies.