Brand new plugin : Category Icons Lite

I wanted it to be simple to use : it does the job all by itself ! No need to search where to put the template tags.

It’ll work with a lot of themes. Just try. If you have an issue, make sure your template has the post title preceded by the permalink and its title :

<a href="<?php the_permalink() ?>" ... title="<?php the_title(); ?>">
<?php the_title(); ?>

Or :

<a title="<?php the_title(); ?>" ... href="<?php the_permalink() ?>">
<?php the_title(); ?>

How to use it :

1. Upload an image as usual via the Media Menu
2. Select the category you want to assign to the image
3. Save the changes

Category Icons Lite

Category Icons Lite

That’s it !

Download it now ! Category Icons Lite

New beta version !

[download id=”3″]

Changelog :

  • Bug corrected : if you had 2 categories which names were similar, you could have the same icon for each category (for example : “Network” and “PlayStation Network”) in the sidebar
  • Bug corrected : in the category page, only that category icon is displayed in front of the title if the post has more than one category
  • Bug corrected : works with multisites
  • Now if there is only one icon to display, the display priority is automatically activated
  • In the Icons management panel, you’re informed if an image is not found or not displayable
  • The put_cat_icons function (used by the widget) works with WP 3.1+
  • You can now choose to get the list from put_cat_icons to process it or to display it (default behaviour). Use this to not display the list :
    put_cat_icons(wp_list_categories('title_li=&echo=0'),'echo=false');

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 category icons side-by-side in a widget

From Daniels (http://www.gameblogs.com.br/) :

I’ve got to make a widget that will show all category icons side-by-side, and not in a list. Here is the code :

$listing_code = '';
foreach(get_categories("orderby=count&order=DESC") as $category) {
$listing_code .=get_cat_icon("echo=false&cat=".$category->cat_ID)." ";
}
$listing_code .= '';
echo $listing_code;

Thanks for sharing, Daniels. 🙂

Update : If you want it in a widget, then you need to create a widget. Copy & paste the following code in a file named myciwidget.php in the default WordPress theme :

<?php

class MyCIWidget extends WP_Widget{
   function MyCIWidget() {
      $widget_ops = array(
         'classname' => 'my_ci_widget', // widget id
         'description' => 'displays category icons' ); // description

      $this->WP_Widget('my_ci_widget', 'My CI Widget', $widget_ops);
   }

	function widget($args, $instance) {
	 	$listing_code = '';
		foreach(get_categories("orderby=count&order=DESC") as $category) {
			$listing_code .=get_cat_icon("echo=false&cat=".$category->cat_ID)." ";
		}
		$listing_code .= '';
		echo $listing_code;
	}

}

Then, in your functions.php file, paste this :

include_once('myciwidget.php');
register_widget('MyCIWidget');

You should be able to add the widget to your sidebar.

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