• I know this has been discussed before, but I’ve not found an answer to it in these forums. Apparently the_category_ID() has been deprecated, so you cannot get the ID of a post with it any more (due to multiple categories being introduced).
    Does anyone know of a way of getting the ID (number) of the category for a post? I only use one category per post so multiple categories are of no concern.
    Any help would be appreciated!

Viewing 15 replies - 1 through 15 (of 19 total)
  • Thread Starter Anonymous

    Ok, I fixed it myself πŸ™‚
    <? foreach(get_the_category() as $cat){ print($cat->category_id);}; ?>

    Thanks for working this out–it was almost exactly what I was looking for!

    Here’s a fun question: is there any way to tweak that code so it will also output given text around it?
    What I am trying to do is be able to have it output the the category ID that matches up with an image file. Right now, on either side of that tag in my template, I have “<IMG SRC=” and “.jpg>” This works fine until I assign more than one category. I was thinking it would be great if we (ok, you) could tweak that code to somehow tell it to add “<IMG SRC=” and “.jpg>” before and after each category ID#. I’ve seen other WP tags that can do something like this, but couldn’t get those tweaks to work on this code. Any advice/help would be much appreciated!

    Thanks, alphaoide–that did the trick!

    Here’s a totally absurd question, but I’m curious if it’s possible to do with the above code–I’d love to have the images shrink by a percentage every time a category is added.
    That’s asking too much, isn’t it?

    Sorry, same problem, but I’m really inexperienced with PHP–where would I put that code?

    This is really weird. I’ve been using the code posted above

    <? foreach(get_the_category() as $cat){ print($cat->category_id);}; ?>

    And it’s working great on all of the posts that were already in my system. On anything new that I post, though, it sees the post as category 27, no matter what it’s actually tagged as. Category 27 is a sub category, but I have several of those in my setup.

    What can I do?

    What version are you using?

    Version 1.2.2

    In that case it could be a subcat thing. I’ve never tried that code on a blog with subcategories so it might be them screwing it up for you.

    Rats, I was afraid of that. What’s weird is that it only affects new posts. Things that were in there before are fine. I’ve been poking around in the DB with phpmyaccess, and there’s nothing obviously amiss. The new posts look just fine.

    By ‘new posts’, do you mean everything posted since you changed the template, or everything since you upgraded to 1.2.2, or has it just started behaving differently for no apparent reason?

    No, it just started behaving differently. The only thing that I can think of that changed is that I installed two traffic tracking packagest, StatTraq and short track (or something like that). I’ve completely removed all of short track and it’s table from the db. StatTraq is deactivated, but the plug-in is still installed and its table is still in the database. It doesn’t ask you to modify any pages, so there’s no extra code.

    Got it working, and this code does seem to work fine in systems with subcategories.

    Well, this is funny–the solution that Alphaoide suggested (and his post) seems to now be missing. Of course, I used it and it has worked quite well. However, now that I am upgrading to 1.5.1.1 it no longer functions.

    Does anyone know how I can get this:

    <?php $catlist = get_the_category();
    foreach ($catlist as $cat) echo $cat->cat_ID ?>

    to spit out an image tag with the proper id number in front of the jpg extension? That code seems to put out what I need, minus the context I need it in.

    In other words, what I’m trying to do is have an image for each category show up based on each category the post is in. I think that explanation makes sense.

    So, if my blog has three categories (1. junk, 2. stuff, 3. things) and a post is in all 3 categories, I should see something like:

    <img src=myblog.com/images/1.jpg><br /><img src=myblog.com/images/2.jpg><br /><img src=myblog.com/images/1.jpg>

    Thanks in advance!!

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Getting the category ID’ is closed to new replies.