How toHow 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

11 thoughts on “How to use Category Icons tags in a script

  1. Pingback: Category Icons » How to find where to place the get_cat_icon tag ?

  2. Is there a way to center the category name under each of the posts, instead of on the right side?

    I need to use the put_cat_icons tag.

  3. I don’t know, perhaps you should play with CSS and the width of the li :
    .myicons li {
    text-align:center;
    width:120px;
    }

    My only advice is to use Firebug to play with that. Good luck.

  4. Hi Sub,
    thanks for this tutorial. I need your help. I am not using the thematic theme and am using some other theme. I want to have a page which will list all categories along with their icons. How can I achieve that?

    thanks.

  5. Thanks you so much for this code. This is almost exactly what I needed.
    I’m using:
    <?
    echo '’;
    foreach(get_categories(“hide_empty=1&orderby=name&order=ASC&echo=0&depth=1&title_li=&child_of=1″) 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 ”.$my_icon.’ ‘.$category->cat_name.’ ‘.$category->description.”;
    }
    echo ”;

    ?>

    How would I make the title into the same link as the icon? ‘.$category->cat_name.’ I can’t seem to figure this out.

    Thanks in advance.

  6. The 6th line should then be :
    echo '<a href="'.get_category_link($category->cat_ID).'">'.$my_icon.' '.$category->cat_name.'</a> '.$category->description;

  7. Excellent plugin.

    I’m pretty new to all this and am trying to figure out how to combine the cat-icon functionality with the category-posts plugin/widget, which allows a listing of posts for a specific category in the sidebar. I would like to add the assigned category icon to the title to make it more obvious what people are looking at. I tried just popping the get_cat_icon() function into the plugin code (which it recognized via if exists…) but nothing is returned. Ideas? Thx.

  8. Hello,

    Is there a way to remove the ‘‘ tag that’s wrapping the img tag in the $my_icon. I only want the img tag because I’m going to replace the href attribute.