WordPress.org

Ready to get started?Download WordPress

Forums

category functions (4 posts)

  1. RobotHero
    Member
    Posted 10 years ago #

    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.

  2. charle97
    Member
    Posted 10 years ago #

    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.

  3. RobotHero
    Member
    Posted 10 years ago #

    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.

  4. pIng
    Member
    Posted 10 years ago #

    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); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.