• johnbillion

    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 4 replies - 16 through 19 (of 19 total)
  • I am interested in the same thing. I can’t seem to even get the category id to echo on my main blog page using Anonymous’ code. I’m running 1.5.1

    <?php
    $catlist = get_the_category();
    foreach ($catlist as $cat) {
    echo "<img src='https://example.com/images/$cat->cat_ID.jpg' alt='some suitable ALT text' /><br />";
    }
    ?>

    Thanks!

    You’re the best. I’ll try this out later tonight and if it sticks!

    That worked.

    I did alter it a little to fit in more with my page. Here’s what I did:

    <?php
    $catlist = get_the_category();
    $cat = $catlist[0];
    ?>

    <img class="your_image_class" src="/path_to_image/<?php echo"$cat->cat_ID"; ?>.png" alt="alt_text" height="45px">
    <?php ?>

    I pulled the function out of the for-each loop to grab just the first element in the array, and then used that for the image. This was to avoid having multiple images for the multiple categories show up in the post info area. Right now I only use single categories for posts, but in the future, if I throw a post into more than one category, I still only want one image.

    Thanks again.

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