The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

the_category_ID() (12 posts)

  1. romulus
    Posted 12 years ago #

    Could someone please help to use the tag "the_category_ID()"? This tag isn't working at all, it should return the ID of the category of the current post (in main loop) but all what I get is alway "0". If I search this forum other people had this problem too. It seems that this function is deprecated. In the wiki though this function is mentioned without a remark that it is deprecated ...
    So how can I get the ID of the category of a post? I want to use this for a categery icon. The icon category plugin posted recently isn't useful for me, because the icon would be placed in the post itself but I want to place an icon near the title.
    So please could give me a hint how to solve this?

  2. NuclearMoose
    Posted 12 years ago #

    Could you please direct me to the page that says the tag is deprecated, so I can deal with conflicting information? Thanks.
    These two links may help you.

  3. Jack
    Posted 12 years ago #

    It seems to be abandoned in the new version. At least that's what I heared. I was struggling with the same thing. With the help of Beel I now have a hack which works fine for me. I also keep a set of icons next to my posts. Take a look at my site, and if this is what you're searching for let me know, so I can send it to you. (leave your e-mailadres in that case... )

  4. romulus
    Posted 12 years ago #

    @nuclearmoose: yes I know these 2 sites, but its not working! I only get zero (0) as the result of the_category_ID() ! And no, there is no page that is stating that this function is depracated, but there were many comments here in the forum that said so.
    After all, maybe its a bug, that this functions returns zero? If so, I'm not into php to confirm this ...
    @moxie: is the second combo box your category chooser? I'm not that good in netherlands ... ;-)
    It looks good to me if I have understood your site structure correct. So please send me your hack to marco(at)romulus23(dot).de I will look into it.

  5. shelleyp
    Posted 12 years ago #

    NuclearMoose, I had a category image display function that also stopped working with version 1.2. I called get_category_link, passing in the parameters (0, $category->category_id, $category->category_nicename), and all I get now is garbage.

  6. steveminutillo
    Posted 12 years ago #

    I ran into the same problem on my site. Now that posts can be multi-category, the 'post_category' column in the posts table will contain zeroes for all newly created posts. Some of the code, especially the template tags, apparently haven't been updated to take this into account.
    I prefer to live in a fantasy world where posts still only have one category, so I created a plugin to work around the problem. It populates the 'post_category' column when I publish a post.

  7. Jack
    Posted 12 years ago #

    I will list the code as I use it now. You can place it in your my-hacks.php and call it somewhere in the posts-loop:
    function the_category_image() {
    $categories = get_the_category();
    foreach ($categories as $category) {
    $category->cat_name = stripslashes($category->cat_name);
    $categoryname = $category->cat_name;
    $picture = $categoryname.".gif";
    $dirname = '../weblog/images/';
    switch($categoryname) {
    case 'groot':
    $alt = 'grotere afbeelding beschikbaar';
    case 'interactief':
    $alt = 'min of meer interactief';
    case 'geluid':
    $alt = 'geluid toegevoegd';
    case 'video':
    $alt = 'videofragment';
    if(file_exists($dirname.$picture)) {
    echo "<img src=\"".$dirname.$picture."\" alt=\"".$alt."\" title=\"".$alt."\">";
    } else {
    This line - $dirname = '../weblog/images/'; - is very important. In my case this refers to http://www.site.com/weblog/images from within my wordpressfolder which is http://www.site.com/wordpress.
    I use this in the case of 4 categories, which in English mean "enlarge" (to give a hint that the image in the post can be clicked for a larger image), and the same info for "video", "sound added" and "interactive".
    The code checks for each category if there is an image in the imagefolder with the name of category.gif. If it exist the code will echo the imagetag, along with the alt and title information.
    And yes Romulus, the second dropdown are my categories. The only thing remains at this moment is that those 4 categories are listed in the dropdownmenu, while they should be excluded. But until now I don't now how and asked about this a few times in this forum but still without an answer.
    Hope this works for all of you. Or at least some...

  8. Beel
    Posted 12 years ago #

    romulus: As I recall that would not work, doesn't the_category() return a link?

  9. Ryan Boren
    WordPress Dev
    Posted 12 years ago #

    the_category_ID() and the_category_head() have been updated in CVS to use the first category in the post's list of categories. If you want the full list of category IDs, use get_the_category().

  10. Ryan Boren
    WordPress Dev
    Posted 12 years ago #

    shelleyp, on the Options->Permalink page, what is your category prefix? The prefix cannot contain any %keyword% strings. It needs to be something static like /archives/category/. We need to add some error checking here.

  11. romulus
    Posted 12 years ago #

    @rboren: thanks! It works now perfectly! :-)

  12. emsdc
    Posted 11 years ago #

    I would love to have the functionality once offered by the_category_ID(). Such a useful function, I can't believe it's no longer available. I understand the multiple category problem, but maybe there could be a way to assign a main category to a post and access that via the_category_ID. Then you could assign other categories to the post but still use the_category_ID to access the main category.

    Is there any way to get this effect? I want to display a different sidebar for my single post pages depending on the what category the post is in, so I need to access the category ID of each single post page.

    I'll go looking for steveminutillo's above-mentioned plugin... maybe that will help.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.