2

I currently have a code where, in my category.php, it checks if the specific category has subcategories, and if it does, show them using wp_list_categories.

I'd love to have these wp_list_categories to show thumbnails, so I created a new walker, but I cannot figure out how to show thumbnails.

I'd be fine with having it show the featured image of my latest custom post type in this category, or using a plugin.

I currently have the following walker in my functions.php:

 class Walker_Category_Parents extends Walker_Category {



function start_el(&$output, $category, $depth, $args) {
    global $wpdb;
    extract($args);


    $link2 = ''.$category->slug.'';
    $cat_name = esc_attr( $category->name );
    $cat_name = apply_filters( 'list_cats', $cat_name, $category );
    $link = '<a href="' . esc_attr( get_term_link($category) ) . '" ';
    $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
    $link .= 'rel="'.$category->slug.'" ';
    $link .= '>';
    $link .= $cat_name . '</a>';
    if ( 'list' == $args['style'] ) {
        $output .= "\t<li";

        $children = $wpdb->get_results( "SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=".$category->term_id );

        $children_count = count($children);

        $has_children = ($children_count != 0) ? ' parent-category' : '';

        $class = 'cat-item cat-item-' . $category->term_id . $has_children;
        if ( !empty($current_category) ) {
            $_current_category = get_term( $current_category, $category->taxonomy );
            if ( $category->term_id == $current_category )
                $class .=  ' current-cat';
            elseif ( $category->term_id == $_current_category->parent )
                $class .=  ' current-cat-parent';
        }
        $output .=  ' class="' . $class . '"';
        $output .= ">$link\n";
        $output .= "<img src='http://localhost/wp-content/themes/vom13/images/producten/";
        $output .= "$link2.png'>\n";
    } else {
        $output .= "\t$link<br />\n";
    }
}




}

As you can see, it currently looks for the image in a folder, but this is absolutely not ideal, because I'm not the only one adding categories, but I don't want to give everybody FTP access.

Any solutions?

Thanks!

0

There's a great plugin called Taxonomy Images. It lets you set one image per category that you can then access in all sorts of ways.

It's a little funky in that it uses some custom filters to return the images, but the documentation is pretty good and you should be able to figure it out.

== UPDATE == Here's a quick example that gets the image based on ID (I'm assuming you'll have the $category object that you already have in your snippet:

$images = get_option('taxonomy_image_plugin');
$cat_id = $category->term_taxonomy_id;
if( array_key_exists( $cat_id, $images ) ) {
    echo wp_get_attachment_image( $images[$cat_id] );
}

Here's a slightly expanded version of that code on the support forums.

|improve this answer|||||
  • Thanks! I have looked into this plugin, but couldn't figure out how to achieve this, and wasn't sure if this was possible. I'll look into it again. – user31532 Apr 16 '13 at 7:03
  • Thanks again for the edit! It shows all the images now, just not on the position I'd like them to be. (But this should be fairly easy to fix, I guess.) – user31532 Apr 17 '13 at 13:39
  • It really isn't cool that you have to load a plugin to do this. I know this is an old thread, but my recommendation is to download the Taxonomy Images plugin as suggested above, open up the code, and copy what you need into your project. This way you don't have all the 'bloat' of the plugin page settings and this and that. WordPress should really make this default / standard. – mrwpress Feb 4 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.