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 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. icon smile How to display category icons side by side in a widget

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

How to display category icons in the sidebar

To display the icons in the sidebar, you have 2 solutions :

1. Use the widget
Go to Appearance > Widgets and drag Category Icons Widget to the right. You can have multiple Category Icons widgets.

Image 71 How to display category icons in the sidebar

Category Icons Widget

Image 8 How to display category icons in the sidebar

Category Icons Widget Settings

2. Manually

In the sidebar.php page of your WordPress theme, you must use the put_cat_icons function.

UA:F [1.6.2_892]

How to assign an icon to a page

In a previous post, I described how to do this with the help of another plugin : Page Category Plus, but this plugin does not exist anymore, so here is another way to do that.

I’ll use the theme “lightword” for this how to. There is 2 parts :

1. Assign a category to a page

When you’re editing the page, go to the Custom Fields, and add : caticons_page as the key, and in the value field, enter the ID of the category to which the icon is assigned to. It’s like assigning a category to a page.

caticon How to assign an icon to a page

The icon is assigned to the category ID 4

Image 21 How to assign an icon to a page

Use the Custom Fields to display a "Page Icon"

2. Use the get_cat_icon tag to display the icon

In the page.php file, after this line : Continue reading

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

How to hide No categories if there are no subcategories

Someone is using the put_cat_icons tag in order to display his categories and subcategories in archive.php. This presents a sizeable icon for each subcategory of the currently selected category. How to do this :

$cat = get_cat_ID( single_cat_title("", false) );
put_cat_icons( wp_list_categories('hide_empty=1&orderby=name&echo=0&depth=1&title_li=&child_of='.get_cat_ID( single_cat_title("", false) )));

The problem is that when there is no subcategory, “No categories” is displayed. So to hide this, we’ll use another filter this time, named wp_list_categories. Paste the following code in your functions.php file in your theme.

function bm_dont_display_it($content) {
  if (!empty($content)) {
    $content = str_ireplace('<li>' .__( "No categories" ). '</li>', "", $content);
  }
  return $content;
}
add_filter('wp_list_categories','bm_dont_display_it');

How to center text vertically after the icons

If you want to center text vertically after the icons, just as I do, you need to add this in your CSS stylesheet (style.css) :

.myicons {
  vertical-align: middle;
}

Add this in your theme files :

get_cat_icon('class=myicons');

If you want to add this in sidebar.php without using the widget, you should have something like this :

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

For the widget, put this in the parameters field :

class=myicons
widget myicons How to center text vertically after the icons

Category Icons Widget

Please, please, use Firebug to test your CSS settings.

How to set a default icon

Suppose you have categories that don’t have yet an icon assigned to. If you want to display a default icon instead of nothing (no icon), here is the code that you must paste in functions.php (in your theme) :

function bm_noicon($content) {
 if (empty($content)) {
   $content = '<img src="http://yourdomain.com/wp-content/uploads/cisco.gif" alt="default icon" />';
 }
 return $content;
}
add_filter('category_icons', 'bm_noicon');

Of course, you need to replace http://yourdomain.com/wp-content/uploads/cisco.gif by your own icon/image URL.

How to assign a specific icon for a post

Someone (onkelandy from Weeds ? icon smile How to assign a specific icon for a post ) asked me if it was possible to assign a specific icon for just a post :

Normally icons are shown depending on the category a post is put in. If a post is related to several categories the priority system comes into place. Anyhow there are some cases where that icon just doesn’t fit to a specific post. So it would be very cool if it was possible to define a specific icon/icon category for just that post by using the “custom field” that appears on the “create post” page in the admin area.

Yes, it is possible, thank to the filter ‘category_icons‘, as I’ve written about in this post. Just make sure you add a custom field named “caticons” and the URL of the icon/image as the value. Paste the following code into functions.php of your theme :

function bm_unic_icon ($content) {
  $values = get_post_custom_values("caticons"); // Get the values of the field 'caticons'
  if (isset($values[0])) {
    $content = '<img src="'.$values[0].'" alt="my specific icon" />'; // Get the icon URL
  }
  return $content;
}
add_filter('category_icons', 'bm_unic_icon',15); // Use the filter 'category_icons'

Of course, it’s up to you to customize the 4th line, in order to make a W3C compliant output, for example…

How to make an horizontal list

Someone asked me how, so here it is. Paste the following code in style.css of your theme :

ul, li {
    display: inline;
}

For example, for the default WordPress theme, it will be :

#sidebar ul, #sidebar ul li {
	display: inline;
}

It’s up to you to find the correct CSS code, as the themes have different stylecheets. My only advice would be to play with FireBug, a FireFox plugin.

How to display a list of the categories with icons

Long time without updates : lot of work and family stuff… But let’s come back to the list. Here is an example of how to do that :

echo '<ul>';
foreach(get_categories("orderby=name&order=ASC") as $category) {
    echo '<li>'.$category->cat_name.' '.get_cat_icon("echo=false&cat=".$category->cat_ID).'</li>';
}
echo '</ul>';

Update : As I’ve received e-mails and comments on how to implement it, here is the solution. First, create a new page and put {#caticons_listing} in it. Then, paste the following code in the function.php file of your theme (if the file does not exist, create one) :

add_filter('the_content','bm_caticons_listing');
function bm_caticons_listing($content) {
    $listing_code = '<ul>';
    foreach(get_categories("orderby=name&order=ASC") as $category) {
       $listing_code .= '<li>'.$category->cat_name.' '.get_cat_icon("echo=false&cat=".$category->cat_ID).'</li>';
    }
    $listing_code .= '</ul>';
    return str_replace('{#caticons_listing}',$listing_code, $content);
}

Update 2 : You can use a shortcode : [caticons_listing], instead of {#caticons_listing}, which results in a simpler code, as suggested by Robert of trupela.com (thanks, Rob !) :

add_shortcode('caticons_listing','bm_caticons_listing');
function bm_caticons_listing() {
    $listing_code = '<ul>';
    foreach(get_categories("orderby=name&order=ASC") as $category) {
       $listing_code .= '<li>'.$category->cat_name.' '.get_cat_icon("echo=false&cat=".$category->cat_ID).'</li>';
    }
    $listing_code .= '</ul>';
    return $listing_code;
}

See a demo here : http://www.trupela.com/2009/11/16/a-post-is-a-category-is-a-story/