How to use a template tag to display icons with Category Icons Lite

You must have at least v1.0.4. Just copy and paste this code in your template file (index.php, loop.php, etc) :

<?php if (function_exists('get_cat_icon_lite')) echo get_cat_icon_lite();?>

You can also give it the category id. If you want to display the icon for the category id number 3, you’d do that :

<?php echo get_cat_icon_lite(3); ?>

How to get only the icons URL

If you want to get only the icons URL, you need to copy and paste this function in your functions.php file :

function extract_caticonsurl($string,$return_always_array=FALSE) {
	$myarray = array();
	if (preg_match_all('/<img\s+.*?src=[\"\']?([^\"\' >]*)[\"\']?[^>]*>/i', $string, $matches, PREG_SET_ORDER))  {
		foreach ($matches as $match) {
			$myarray[] = $match[1];
		}
	}
	if ($return_always_array === FALSE && count($myarray) == 1 ) $myarray = array_pop($myarray);
	return $myarray;
}

And paste this code where you want to get the URL(s) :

$caticons_url = extract_caticonsurl(get_cat_icon('echo=false'));

The extract_caticonsurl function returns an array if there’re several icons, or a string (the URL) if there is only one.

How to display icons for pages in the sidebar

I’ve written a function that seems to be working. I think I’m going to include it in a future version of my Category Icons plugin. So, you can have page icons the same way you have category icons in the sidebar.

The following code will add icons in front of the title page in the sidebar when you use the Pages widget. You can see that I’ve used a filter named widget_pages_args to add the filter wp_list_pages in order to parse the HTML generated by wp_list_pages. It’s because I want to display icons only in the sidebar and nowhere else.

To use that code : Continue reading

Icons for this blog

As you can see, I don’t have icons for my categories. I’ve been searching around for icons, but unfortunatly, I couldn’t find ones corresponding to my categories.

So I’m searching for a volunteer who would create icons for my categories :

Documentation, How to, FAQ, Translations, Changelog, News, Tips & Tricks

If someone wants to help, you’re welcome !

What is Category Icons ?

I agree with Ryan Caldwell (performancing.com) :

So the first step in my adventure was to prettify [my pages] a bit. To do this, I downloaded the Category Icons plugin for WordPress. Category Icons allows you to associate a unique image with each of your WordPress categories. By placing a little snippet of code on your category.php theme file, you can then have a big, bold image come up that will make your category pages look better than 90% of the category pages out there.

Or with  JTPratt when he wrote :

It’s a beautiful plugin [...] well documented and supported. Everything is configurable from the options menu, you can use it on your index (home) page, post pages, or even your sidebar! You can do icons only, or text and icons, and it comes with a widget for the sidebar.

The cool thing about the plugin is that it gives you ways to do one or multiple category icons, but also a method for putting icons with your categories in the side bar as well ! I’m always very grateful when a good plugin come along !

icon biggrin What is Category Icons ?

Sources : JTPRATT’s Blogging Mistakes ( here & here ) & Performancing

How to display icons in posts

1. download the plugin and install it.
2. Upload your images to your upload folder. The size of the icons depends on your needs & theme. There’s no recommanded size to use the get_cat_icon() function.
3. Assign icons to your categories in Manage tab.
4. Then, in Template Code, the plugin will show you where to put the get_cat_icon() and put_cat_icons() functions in your theme files :

templatecode How to display icons in posts
5. You can also use the widget to display icons in your sidebar.

Note that the ‘small’ icons are displayed by default. If the small icons are not found, the normal icons will be displayed, and vice versa. If you have both type (normal and small) and you want to display ‘normal’ icons, you must use set the parameter ‘small’ to false.

Example :

I want to display ‘normal’ icons in my posts. I’ll put that code in index.php (for example) :

get_cat_icon('small=false');

How to assign an icon to a category

In WordPress 2.3.X :

Go to Manage > Category Icons and click on Icons tab.

manageicons How to assign an icon to a category

In WordPress 2.5 :

Go to Settings > Category Icons and click on the Icons tab.

manageicons25 How to assign an icon to a category

In WordPress 2.7+ :

Go to Posts > Category Icons and click on the Icons tab.

iconstab How to assign an icon to a category

Icons tab

If you want to delete the icons of a category and its priority, click on the checkbox in front of it, and then click on the button “Delete Icons and Priority”.

In the “Name” column, click on the name of the category to edit or select its icons.

selecticons25 How to assign an icon to a category

In the dropdown menu icon or Small icon, select an icon among the ones you uploaded in the default folder (wp-content/uploads) or the folder you’ve choosen to upload them. And then click on the button to the right to validate.That’s all.

Icon priority by Oliver Weichhold

You can prioritize the display of icons. From the author (Oliver) :”You can now assign numeric priorities to categories. The priority is used to select a single category icon for a post with multiple categories.Example:

imagine we have a post with two categories:

  • Companies -> ACME Corp. (Icon: ACME.gif, Priority 10)
  • Products -> Milk (Icon: Milk.gif, Priority 20)

If you now pass the new boolean parameter “use_priority=true” to the get_cat_icon() function, it will only render Milk.gif, because Products -> Milk has the highest priority of all categories for the post”.

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 :

Image 1 Function : put cat icons()

Widget

Display icons vertically

You can now display the icons vertically. It’s like a ’stack’ of icons, thank to a new parameter for the function get_cat_icon() : vertical_display. Set it to true if you want to use that feature.

Example :

<?php get_cat_icon(‘vertical_display=true’); ?>

The ’stack’ is generated thank to a div tag. That div tag has a class : caticons. So if you want to extend the style of the div tag, you can use css in style.css.

Example of generated code :

<div class=”caticons” style=”text-align: center; float: left; width:32px“> [the_icons_are_here] </div>

Example of extending the class in style.css :

img.caticons {
border
:0 !important;
}

You can’t modify the value text-align, nor the float value. The width take automatically the larger width of the icons to display.

Function : 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.