WordPress.org

Support

Support » Plugins and Hacks » category functions

category functions

  • I want to apply a different layout to one category of post. I’ve been looking at the category functions, and I’m not sure what’s the easiest way to test, “Is this post of category X?”
    the_category_ID is always returning 0, so I don’t know if I’m using it wrong.
    <?php the_category_ID() ?> always prints 0
    Unless someone suggests a better way, I’m going to try writing my own function for this, which will use get_the_category() and test to see if a certain category is returned in the results.

Viewing 3 replies - 1 through 3 (of 3 total)
  • write your own function to pull category ids. the_category_id function is trying to pull a post’s category id from a field that is apparently deprecated.

    Okay. I added this to myhacks.
    function the_category_IDs() {
    $IDs = array();
    $categories=get_the_category();
    for ($i=0;$i<count($categories);$i++)
    {
    $IDs[] = $categories[$i]->category_id;
    };
    return $IDs;
    }

    And then anywhere I want to test to see if a post is in a category, I just use if (in_array(X,the_category_IDs())) where X is the category ID I’m testing for.

    Instead of using complex hack code, I chose to simply search the query string for the category number, then I’ve inserted it inline where needed to modify image or css tags. I’m no PHP guy, but this works great for me and it’s one line of code:
    <?php echo($cat); ?>

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘category functions’ is closed to new replies.