How to use Category Icons tags in a script

Sometimes, you need to use the tag get_cat_icon or/and put_cat_icons in a script. So to get the icon in a variable, it must not be displayed. The parameter to not display the category and to get it returned is : echo, and it needs to be set to false. Here are examples of how to use it. If your script is not in the Loop, you must use the cat parameter and give it a category id. Continue reading

Function : put_cat_icons()

This function “iconizes” your categories list in the sidebar.

put_cat_icons(list [,get_cat_icon_parameters] [,’icons_only=true’])

  • list : this is a WordPress category list : wp_list_categories(). You MUST ADD echo=0 to the wp_list_categories parameters.
  • get_cat_icon_parameters : (optional) these are the get_cat_icon() parameters, separated by a ‘&’.
    There’s an exception : for put_cat_icons(), the parameter ‘link’ is always set to false.

Example :

write the following code in sidebar.php to have icon next to the category name for which you uploaded an icon. You can give any parameters to wp_list_categories, but you MUST ADD echo=0, in order to process the list :

if (function_exists('put_cat_icons'))
  put_cat_icons(wp_list_categories('title_li=&echo=0'));
else
  wp_list_categories('title_li=');

In the parameters, if you put : ‘icons_only=true’, then only the icons will be displayed in the sidebar, instead of the icon AND the category name. Look at the 2nd line :

if (function_exists('put_cat_icons'))
put_cat_icons(wp_list_categories('title_li=&echo=0'),'icons_only=true');
else
wp_list_categories('title_li');

If you use the widget, just put icons_only=true in the parameters :

Widget

Widget