WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Alt. for in_cat_hierarchy()? (2 posts)

  1. seluna
    Member
    Posted 7 years ago #

    I use WP as a CMS for my site, and in doing so, I have a lot of posts in different multiple child categories. To help correctly identify and display the posts, I added the following code that was placed up here on the support forum by a member 'ringmaster', which sort of broke down after I upgrade to 2.1.

    I kind of figured out what really went wrong, but I don't know how to fix it. $categories needs to be an array for foreach loop to work, but somehow it's just not becoming one. I've confirmed that $category_cache is an array, since I managed to get it to show me its keys and values.

    Please help me out, my website can't work (and look) well without this code.

    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]);
    }

  2. seluna
    Member
    Posted 7 years ago #

    I found the solution myself by referencing the top part of the category-template.php... *sweats*

    function in_cat_hierarchy($cat, $postid = '') {
    global $category_cache, $cache_categories, $post, $blog_id;
    $incats = array();
    if ( !$id ) {
    $id = $post->ID;
    }
    update_post_category_cache($id);
    $categories = $category_cache[$blog_id][$id];
    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]);
    }

Topic Closed

This topic has been closed to new replies.

About this Topic