• When I use this code <?php the_category_ID(); ?> in my indexpage, it always returns ‘0’ (zero)?
    To be sure I also tried <?php echo the_category_ID(); ?>, but with the same result.

Viewing 8 replies - 1 through 8 (of 8 total)
  • try <?php the_category_ID($echo=true);>
    If that does not work, the feature might have been deprecated.
    Open wp-includes/template-functions-category.php and look for this function, find if it’s there, and use it as described there, if it’s there.

    Also, the function uses $post->post_category, so it only means something when you are within the loop, talking about a particular post. (it won’t work in the menu div, for example, but will work on the individual entry pages)

    Thread Starter Jack

    (@moxie)

    I wanted to use this code to attach an image to certain categories and tried it out first, but it kept returning ‘0’. In this example you can see that it’s within the loop, because after the date of the posts.
    <div class=”datum”><?php the_time(“l j F, Y”); ?></div>
    <div class=”catimages”><?php the_category_ID($echo=true); ?></div>
    I wanted to use this to show images with certain categories. Is there a better way to do this? This would be difficult because each post is assigned to at least two categories.

    Evidently the post_category value has been deprecated due to the new wp_post2cat table. However, next_post() still uses this value for the $in_same_cat variable, which, of course, results in every post appearing to be in the same category (cat=0). Does anyone have new code for next_post() that actually works (in regards to $in_same_category)? As best as I can figure, one would have to select post_id from wp_posts, compare it with category_id in wp_post2cat and (if post_id > current_post_id), return the link. Which could possibly be as ugly as it sounds. I hope there is a better way.

    Boy, you never ever deprecate old functions without giving people a heads up.
    And you never ever do this without a lot of advance warning.
    And you want to think about wrapping the old functions to make them work in the new environment.
    Ever. Open source or not, you never ever do this without making some public note of the fact.
    2fargon, where is the discussion on this? In the IRC channel? Buried in the support forum?

    This is fixed on CVS HEAD and will be fixed on the 1.2.1 branch shortly. the_category_ID() is not multiple category aware, so get_the_category() should be favored instead. So, it is informally deprecated, but deprecated does not mean it should be broken or missing. This is just a bug.

    The same issue is present in the next_post and previous_post functions also. There is no join to the wp_post2cat table.

    I use the following code to determine categories. The variable “$catnews” is something I came up of course and the “2” is simply the category id of my news section.
    <?php
    $catnews = in_category(2);
    if ($catnews == ‘true’) {
    echo “this is the news”;
    } else {
    echo “this isn’t the news”;
    }
    ?>
    This is a more involved version:
    <?php
    $catnews = in_category(2);
    $catmusic = in_category(3);
    $catgallery = in_category(4);
    $catwritings = in_category(5);
    $catbio = in_category(6);
    $catcomrades = in_category(7);
    if ($catnews == ‘true’) {
    echo “this is the news”;
    } else if ($catmusic == ‘true’) {
    echo “this is the music section”;
    } else if ($catgallery == ‘true’) {
    echo “this is the gallery”;
    } else if ($catwritings == ‘true’) {
    echo “this is the writings of Yesha Cohen”;
    } else if ($catbio == ‘true’) {
    echo “this is the biography page”;
    } else if ($catcomrades == ‘true’) {
    echo “these are comrades”;
    }
    ?>
    I’m only using this code on the index page as a test before I try to implement multiple templates based on their category.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘the_category_ID shows no ID’ is closed to new replies.