DocumentationFunction : get_cat_icon()

The size of the icons depends on your needs & theme. There’s no recommanded size to use the get_cat_icon() function.

The function get_cat_icon() return boolean True if an image is found. You can use the following parameters.

type parameter summary
boolean align align attribute for the image tag
boolean border If true, displays the icon with a border. If false, no border is diplayed. (Don’t use this if you want valid Strict XHTML)
int cat Category ID. If not specified, the current category is used or the current post’s category.
string class Class attribute for the image tag.
boolean echo If true the html code will be ‘echoed’. If no, it’ll be returned. Default : true
boolean expand Whether the image should be expanded to fit the rectangle specified by fit_xxx. Default : false
int fit_height Maximum height (or desired height if expand=true) of the image. Default : -1
int fit_width Maximum width of the image, or desired width if expand is true. Default : -1
boolean hierarchical If true, displays the icon in hierarchical order. Default : false
boolean link If true, an anchor tag wraps the image (a link to the category is made). Default : true
int max_icons Maximum number of icons to display. Default : 3
string prefix String to echo before the image tag. If no image, no output.
boolean small Use the small icon. Default : true.
string suffix String to echo after the image tag. Ignored if no image found.
boolean use_priority If true, only the most prioritized icon will be displayed. Default : false

Examples

This example will insert the icon associated with the current category:

<?php get_cat_icon(); ?>

Here is an example of how to use it in index.php of your theme directory :

<div class="left_side">
<?php
if (have_posts()) :
  while (have_posts()) : the_post();
    if (function_exists('get_cat_icon'))
      get_cat_icon('fit_width=32&fit_height=20&small=true&class=myicons');
?>

Example of class attribute for the image :

.myicons {
margin : 0 3px 0px 0;
}

This example inserts icon for the category with ID ‘5′. The image is fitted withing rectangle 100×100. If the image is smaller than desired, it is expanded :

<?php get_cat_icon('cat=5&fit_width=100&height=100&expand=true'); ?>

More examples.

If you’re wondering : where to put the get_cat_icon() code, read this.

35 thoughts on “Function : get_cat_icon()

  1. Disculpen la molestia, pero yo tengo problemas con el category icons, cuando aparece el icono (que es de 16×16) le agrega un espacio blanco a la derecha y abajo, haciendo que el espacio mida 41×41, por que pasa eso?

    gracias :D

  2. Just look for the little code get_cat_icon() in your php page, then cut and paste it wherever you want.

  3. I am missing something? What file names do i need to name the images? And they should be in uploads as stated right? Ive tried to name by category name, category id, and variations and no luck.

    Basically i just want to be able to put a call to in my category template to display a category image unique to it, i.e i name the images cat-1 and category 1 shows that image.

    I know this plugin offers more features, but im just trying to get that down, and ended up trying to use this for the job, any help would be much appreciated.

  4. Have you read this :
    http://www.category-icons.com/2008/03/how-to-display-icons-in-posts/
    You could also read some how-to :
    http://www.category-icons.com/category/howto/

    To resume the process : once you’ve uploaded your images (icons), you MUST assign an icon to a category. Then, with the help of Template Tag, you can insert the get_cat_icon() function in your template files.
    The icons will be displayed automatically.

    Now, if you don’t want to use the get_cat_icon() for general purpose (automatically), you can use the parameter ‘cat’ to display the icon of a particular category. For example, if you want to display the icon of the category numbered 5, do this :
    get_cat_icon(‘cat=5′);

    Thanks to read the documentation and the how-to.

  5. I’ve quite a situation using this plugin with a new template.
    The icons are mostly black and most of the template is white or white-like, with the exception of the sidebar, that is black.
    So I’m wondering if there is a way to get the icons in a different way. Let’s say, adding a parameter that appen a scring to the name:
    get_cat_icon(’suffix=_white′);

    I know that this parameter doesn’t exists, but is just to give an idea of my needs.
    I could also play with CSS, but I don’t think would be a clever solution.

  6. Hi there,
    Congratulations for your plugin ! I have a problem while using it :
    I have an error message on the admin page ” Erreur dans l’extension ‘Category Icons’ : le répertoire de téléversement (upload) par défaut n’est pas accessible. Veuillez en créer un, ou alors changer les droits du répertoire.” which (partially) means “upload folder is not accessible, please create one or change permissions”
    whereas I have the “uploads” folder created and it already has a 755 permission
    Can you please help me ?
    thxs a lot !

  7. Salut amo. Il faudrait que tu mettes les droits en 777 d’abord, histoire de voir que ton problème ne vienne pas des permissions… Et vérifie bien que dans Réglages > Divers, il y ait bien wp-content/uploads.
    If none of that works, please have a look to the troubleshooting page.

  8. This does not work for me on a category page. Instead of displaying the current category it displays the first category from the first post on the category page. I am displaying title and description for the current category and want the current icon as well. Is this a bug or am I missing a setting? Thanks.

  9. Hi everyone, I absolutely love the get_cat_icon() plugin! I use it all of the time, however I was never really happy with the quality of the image resizing since it relied on the browser to scale the image. What I have done is modified the plugin a bit to utilize awesome Tim Thumb PHP script to automatically resize the images. Check out mywebsite to see how clean the images look: http://interiordesigndeals.com

    To make the category icons plugin work with Tim Thumb, follow these steps:

    1.) Install Tim Thumb by following these instrucitons: http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/
    2.) Resizing an image is super easy, follow this syntax:
    2.) Modify the category_icons.php file (around line 237), to look something like this: $cat_icons .= ‘src=”/scripts/timthumb.php?src=’.$url.’&h=’ . $h . ‘&w=’ . $w . ‘&zc=0″ alt=”" ‘.$title.’ />’.$urlend.$p['suffix']; This will take in the category_icons fit_width and fit_height variables and use it to resize your image to the proper heigth/width.

    Disclaimer: I’m not a plugin developer, so I hope that my code modification will not break the plugin. Please feel free to comment back if you find a better way to accomplish the same thing.

    Have fun!

    - Bill

  10. Good job ! Thanks ! I’ll have a deep look at this. That’s a very good render.

  11. This plugin seems to work for blogging categories in word press. Is it possible to make it work for a Page in wordpress too? Such as an icon for the page?

  12. Thanks for the great plugin. I’m not sure this functionality isn’t in the WordPress core, but I’m glad I found this plugin.

    I believe that the default for the small (icons) is true, not false. ie. by default the function returns small icons not the large ones.

    I also would like to request an enhancement. It would be great to be able to specify an icon set for pages categories as well. That way our pages could have small icons instead of bullets in the sidebar and if we want we could place the larger icons on the individual pages themselves.

  13. @Tim
    Hi. Thanks. I’ll look at this small/normal icon size when I’ll have some spare time. I can’t add new feature right now : I have a job (and a life :) ) that takes me a lot of time.

  14. What variation of the function get_cat_icon do I need to return just the url of the large icon?

    I am using this in my css file for a header image, so the only thing I want returned is the plain link to the image with no a href= etc.. just the url of the large image….

    Basically I created a basic function that determines if there is a cat icon, and if so then that url is displayed in my php css file.

    example:
    background:#000000 url() no-repeat scroll center 0;

    you can see that custom_header_background needs to return the url of the large image only.

    I will try to paste in my function here, though I doubt it will take.
    Basically it says look for the cat image and if it’s there display that, else display my default header image.

    function custom_header_background() {if (function_exists(‘get_cat_icon’)) { get_cat_icon(‘small=false&link=false’); }
    else {echo standard_header_image();
    }
    }

    So far the function works, but it’s returning way to much information for my purposes.

    I did look through the documentation but did not see the ability to return just the url string.

    thank you

  15. argghhh my css example above did not take, lets try that one more time

    background:#000000 url() no-repeat scroll center 0;

  16. ok i give up….

    basically I am just using a php echo custom_header_background statement in my css php file there. Not going to try and paste it again, as both tries did not work. I’m sure you get the idea though.

  17. @shawn

    Somewhere in your function, put this :
    $caticon_url = extract_caticonsurl(get_cat_icon('echo=false'));

    and add this function :
    function extract_caticonsurl($string) {
    $result = '';
    if (preg_match_all('/<img\s+.*?src=[\"\']?([^\"\' >]*)[\"\']?[^>]*>/i', $string, $correspondances, PREG_SET_ORDER)) {
    $result = $correspondances[0][1];
    }
    return $result;
    }

  18. Hello. Looking for some support on something.

    What I am trying to do is display a list of categories with their description and icon. The problem is, the icon code is only applying one image/link to every category, how can I fix this?

    Code:

    <?php $categories = get_categories('number=10');
    foreach ($categories as $cat) {
    echo "term_id ) . “\”>”. $cat->cat_name . ““. $cat->description . “” . get_cat_icon();
    } ?>

    Another problem is I am getting a “1″ below the image. :/

    Thanks,
    Tom.

  19. Hi, i wnat to display of the category in front of the icon, but in the middle of the icon, not in the bottom, Thanks

  20. Hi–I’m using a non framework theme (Studiopress’s Genesis, plus a child theme). What modifications would I need to make to implement this into my theme?

  21. @Tatiana

    Did you try the Category Icons Lite plugin ?

    Or else, if you want to use the Genesis theme, did you use the template tag tab to view where you should paste the get_cat_icon() tag ?

  22. Hi !

    How to… not show one parent categorie ?
    Say that I have :

    Cat1
    - Sub1
    - Sub2
    Cat2
    - Sub1
    - Sub2

    How can I only show only Cat 2 (and Sub) ? If you put cat=2 it only show the Cat2 icons on every page, I tried cat=-1 but same thing. I want this to be automatic as usual, but not showing some categories…

  23. Second comment, just had an idea. Is it possible to show icon just from one priority ? Because the one I want to show are “0″ and those I don’t are “1″… so if I can exclude 1…

  24. Hi, I have 2 questions:

    1- Can I exclude some categories with a parameter?

    2- Can I use the parameter “cat=” to include more than one category?

    Thanks

  25. How can I get the alt tags populated for each icon? The alt=”" is blank, not the best for SEO etc…