WordPress.org

Ready to get started?Download WordPress

Forums

Getting the post's catID (17 posts)

  1. TechGnome
    Moderator
    Posted 9 years ago #

    According to the Wiki documentation the_category_ID() will return the category id the post is in.

    The ID (number) of the category the post belongs to.

    How ever, it seems to always return 0, no matter what I do. I wanted to use it to check to see if the post is in one particular category (it will be the only category the post(s) will be in, so multi-cats aren't an issue - I think) and if so, I want to change how some of the post meta info is displayed.
    So what's the deal? Does it not work, or is it left over legacy code from the single cat days?
    Does anyone have any ideas on how to check the cat id of a post? If there are more than one, I know immediately that it isn't in the special category, so that helps some, but I'd say that 80% of my posts are single cat posts, so that's only going to work once in a while.
    Any clue?
    TG

  2. charle97
    Member
    Posted 9 years ago #

    try in_category();
    example is here:
    http://photomatt.net/archives/2004/05/19/asides/

  3. TechGnome
    Moderator
    Posted 9 years ago #

    You know, I saw that earlier and it just didn't click ..... now it does... Thanks, I'm going to give it a go.
    *fingers = crossed*
    TG

  4. TechGnome
    Moderator
    Posted 9 years ago #

    well, that didn't seem to work. The function doesn't appear to return anything.
    TG

  5. charle97
    Member
    Posted 9 years ago #

    how are you using it?

  6. TechGnome
    Moderator
    Posted 9 years ago #

    Like so:

    <?php if (in_category(-1)) { ?>
    ... hide the meta info for the post ...
    <?php } else { ?>
    ... show post meta info ...
    <?php } ?>
    OK, now, I have to admit that there is no such catID as -1 in my database, I'm using it as a pseudo hidden category, I've got a plugin that sets it to -1 based on a setting.
    The catID is in fact -1 -- I checked the database itself ala phpMyAdmin just to make sure.
    But even when I put in 1 or 3 or 7 in the function call, just to see, I got nothing.
    TG

  7. charle97
    Member
    Posted 9 years ago #

    are you using the function within the Loop?

  8. TechGnome
    Moderator
    Posted 9 years ago #

    Yes I am.....
    Inside the loop, where the post meta info is procedded - "Filed Under: X, Y, Z By: Author at 12:43PM, etc..." That's the stuff I'm trying to deal with. When I'm displaying a post of Catid = -1, I want to not display the meta info, but if it's a normal post, then I do want the meta. Is this making any sense?
    TG

  9. charle97
    Member
    Posted 9 years ago #

    are you using 1.2?

  10. TechGnome
    Moderator
    Posted 9 years ago #

    1.2 Mingus..... I even found the function in the wp-includes/templates-functions-post.php file.
    TG

  11. charle97
    Member
    Posted 9 years ago #

    i have no clue what the problem could be.

  12. charle97
    Member
    Posted 9 years ago #

    try something simpler without the if statement and the -1. maybe just echo the function.

  13. Anonymous
    Unregistered
    Posted 9 years ago #

    the_category_ID hasn't worked for a while now:
    http://mosquito.wordpress.org/bug_view_page.php?bug_id=0000096
    The guys on this thread sound like they might be trying to do something similar to you but don't know if they ever got a solution :(

  14. TechGnome
    Moderator
    Posted 9 years ago #

    charle97 - that was the first thing I tried - nada.
    Anon - what the---..... how in the world is Matt using the damned thing then? grumble, grumble.......
    *kicks a stone off the cliff*
    TG
    GAH! I jsut checked the bug you linked to.... it's friggin depreciated... no wonder it doesn't work. For crying out loud. Puts me bac kat square one.

  15. TechGnome
    Moderator
    Posted 9 years ago #

    Hey all, I appreciate the help, but I've given up. Nothing works. I was hoping to do this w/o having to modify any core files. Since none of it works, I'm not going to be able to do what I want. I thought about using the_category and chec kt osee if anything is passed back, but it echoes hte results instead of returning it.
    *sigh*
    Damn shame. Had such high hopes.
    Adios.
    TG

  16. charle97
    Member
    Posted 9 years ago #

    try this:
    http://wordpress.org/support/3/4801#post-28758
    i used that before matt coded in_category.

  17. TechGnome
    Moderator
    Posted 9 years ago #

    charle97 - you are a life savior! The code in that post didn't work for me either, but at least I found out why. The in_category() and the other code were dependant upon get_the_category working right. In my case it doesn't because the catID doesn't exist in the categories table. Once I was armed with that info, I made a function that looks at the number of elements returned by get_the_category() .... if it's 0, then it belongs to my special cat, and I return True, else I return False.
    Now it works like a champ!
    Thanks very much for not giving up on me.
    TG

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.