Skip to main content
2 of 10
added 2906 characters in body
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109

Instead of using get_categories(), I'd suggest you take a look at wp_list_categories(). You can get the same output with it, but it has the benefit to be highly customizable.

There are two ways for customization, either via the filter hook wp_list_categories - see in source - or by extending the Walker_Category class - see in source. The latter gives you much more possibilities for customizing, but might be over the top for minor changes. So you have to evaluate what fits your needs best.

Because this topic is actually pretty well covered on WPSE and SO, I'll just give you a short list of references. Besides that you have not given a specific enough description of your needs, it remains a bit vague, although one can know what you want. Anyway, below resources should help you to chose the method fitting for you and additionally should enable you to achieve it.

  1. filter hook wp_list_categories
  1. Walker_Category class

One more remark, personally I would almost always opt for the extending the Walker_Category class method, but for cases where the change you want is very minor. This is of course a bit of personal preference, but has some background, particularly because of re-usability, extendability and - even if it doesn't necessary seem like it - after all it often - maybe even most of the time - is the easier way to achieve the custom output.


Update:

I've given the plugin - Category Thumbnails - I mentioned in the comment above a quick test. It's generally working and I feel it's a better solution than what you're proposing in your answer, because it enables you to manage the thumbnails via the wordpress media library.

I discovered a minor bug, the plugin author is notified and replied instantly, so the bug will be fixed soon. If you can't wait you can fix it yourself like this:

However the function

the_category_thumbnail();

doesn't produce a output. it looks like this in the source code of version 1.0.3 :

function the_category_thumbnail($category_id = null, $sizes = array()) {
    print get_the_category_thumbnail($cat, $sizes);
}

I managed to fix the problem like this:

function the_category_thumbnail($category_id = null, $sizes = array()) {
    print get_the_category_thumbnail($category_id, $sizes);
}

So, by making the parameter/argument name equal.

Or you just use get_the_category_thumbnail() instead, you just have to echo it:

echo get_the_category_thumbnail('1');

Another minor disappointment is that the plugin and functions it offers doesn't give you the possibility to work with the generated image sizes. Which isn't that good, because we want the correct size and not always the full size. I'm going to notify the author about that too, so he can improve his plugin further. In the meantime this luckily can easily be fixed by constructing a custom function:

/**
 * wpse135208_cat_thumb_wrapper.
 *
 * Wrapper function to have the ability to use predefined image sizes via wp_get_attachment_image().
 *
 * @since Fr 28 Feb 2014 17:54:02 
 * @version 0.1
 *
 * @link http://wordpress.stackexchange.com/q/135208/22534
 *
 * @param integer $category_id (default: null)
 * @param string/array $size (default: 'thumbnail')
 * @param boolean $icon (default: false)
 * @param string/array (default: null)
 *
 * @return string
 */
function wpse135208_cat_thumb_wrapper( $category_id = null, $size = 'thumbnail', $icon = false, $attr = null ) {
    $category_thumbnail_obj = get_the_category_data( $category_id );
    return wp_get_attachment_image( $category_thumbnail_obj->id, $size, $icon, $attr );
}

Usage like this:

echo wpse135208_cat_thumb_wrapper( '1', 'thumbnail', false, null );

This should help you to get this working. Like I said, it's preferable because you can manage your category thumbnail via the media library. In your code use it like shown below:

$categories = get_categories($args);
foreach($categories as $category) { 
    echo wpse135208_cat_thumb_wrapper($category->cat_ID); 
} 
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109