WordPress.org

Ready to get started?Download WordPress

Forums

comment ID: how do I get it in 1.5.1.1? (11 posts)

  1. thepete
    Member
    Posted 8 years ago #

    I've scoured the forum and have found numerous posts relating to how to get the category ID in previous versions of WP but I can't find any mention of how to get it in 1.5.1.1 (which is what I'm running). The codex says the old category_ID tag is deprecated (which I thought meant something else) but since the WP admin interface still displays category IDs there must be some way to get them. Link categories also use them, so why was this function made obsolete?

    Anyway, ideally what I need is a way to display each category ID for a given post with an image tag wrapped around each ID, like so:

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

    That's with "1" "2" and "3" being the category IDs for a given post.

    Thanks in advance for any advice!!

  2. thepete
    Member
    Posted 8 years ago #

    Damn, I meant to post "Category" in the title, not "comment". I didn't notice until the post was too old to edit. Sorry about that.

    Is there maybe an admin that can change that to "Category" so I might actually get replies?

  3. Firas
    Member
    Posted 8 years ago #

    <?php
    foreach((get_the_category()) as $cat) {
    <img src="http://myblog.com/images/<?php echo $cat->category_id; ?>" />
    } ?>

  4. Firas
    Member
    Posted 8 years ago #

    Oops. Rather:

    <?php foreach((get_the_category()) as $cat) { ?>
    <img src="http://myblog.com/images/<?php echo $cat->category_id; ?>" />
    <?php } ?>

  5. Firas
    Member
    Posted 8 years ago #

    Just not my day :(. Final form:

    <?php foreach((get_the_category()) as $cat) { ?>
    <img src="http://myblog.com/images/<?php echo $cat->category_id; ?>.jpg" />
    <?php } ?>

  6. thepete
    Member
    Posted 8 years ago #

    Thanks for your attempts Firas! Alas, none worked. Sorry about that.

    I even tried tweaking it a bit with my meager php knowledge, but no luck. Thanks for trying though!

  7. Firas
    Member
    Posted 8 years ago #

    That'll teach me to paste untested code :/

    Here you go:

    <?php foreach((get_the_category()) as $cat) { ?>
    <img src="http://firasd.org/images/<?php echo $cat->cat_ID; ?>.jpg" />
    <?php } ?>

    Blame the codex (http://codex.wordpress.org/Template_Tags/the_category_ID#Replaced_With) :) I'll fix it tomorrow.

  8. thepete
    Member
    Posted 8 years ago #

    Thanks, Firas! You are a prince among men!!

    Uh, I'm guessing that you're a guy, anyway...

    Oh and your site is gorgeous. I wish I had your design sense. Very neat and tidy. Just redesigned my site basing it on Kubrick because my last site design was even more clutered than I managed to make Kubrick.

    Anyway, thanks again for your help!

  9. Firas
    Member
    Posted 8 years ago #

    Learn all about get_the_category here.

    Thanks for the comments on my design, it's a slight modification of the fantastic Persian by Chris J Davis.

  10. Jinsan
    Member
    Posted 8 years ago #

    nice work firas - however I have come across a validation problem, which is you need an alt attribute for an image. how would you generate an alt tag, even if's just a dummy (can you even use the same alt tag for all images?)

  11. Firas
    Member
    Posted 8 years ago #

    Nice catch. Changed it to the category name (I personally would set it to an empty alt attribute because I use the_category() to list the cats anyway).

Topic Closed

This topic has been closed to new replies.

About this Topic