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.

1. The get_cat_icon tag

The example below displays icons with their category names and descriptions. Copy and paste the following code in index.php or another page outside the Loop :

echo '<ul>';
foreach(get_categories("orderby=name&order=ASC") as $category) {
  // Get the icon in a variable
  $my_icon = get_cat_icon('echo=false&cat='.$category->cat_ID);
  // Display a list with icons and the category names
  echo '<li>'.$my_icon.' '.$category->cat_name.'<i><font color="#AAA">'.$category->description.'</font></i></li>';
}
echo '</ul>';

The result :

Use get_cat_icon tag in a script

Use get_cat_icon tag in a script

2. The put_cat_icons tag

If you want to use put_cat_icons in a script, you must also give the echo parameter. Here is an example (for Category Icons version > 2.1.1) :

// Get the list in a variable
$my_icons_list = put_cat_icons( wp_list_categories('echo=0&title_li='),'echo=false');
// Append the word category to the category name
$my_icons_list = str_ireplace('</a>',' category</a>',$my_icons_list);
// And then display it
echo $my_icons_list;

The result :

Use put_cat_icons tag in a script

Use put_cat_icons tag in a script