Ask your WordPress questions! Pay money and get answers fast! (more info)

Displaying Thumbnails of child-categories on parent-category page WordPress

  • SOLVED

I've tried to find a solution to this problem, but just cannot figure it out :-(

I want to display the thumbnails of all child-categories on category.php
I already installed the plugin "Taxonomy Images" and was able to give a thumbnail to every child-category. Now I wonder how to implement the thumbnail into my loop:

<div class="entry-content">
<?php
$cat = get_query_var('cat');
$cat_list = wp_list_categories('child_of='.$cat.'&hide_empty=0&title_li=&echo=0&orderby=ID');
;
if($cat_list) {
echo 'Child-Categories: <ul>';
echo $cat_list;
echo '</ul>';
}
?> </div><!-- .entry-content -->

If you know any better solution (loop) or plugin to work with, please don't hesitate to tell!
Many thanks in advance!

Answers (2)

2017-06-14

Rempty answers:

are you using this plugin?
https://wordpress.org/plugins/taxonomy-images/
You can use this

$cat = get_query_var('cat');
$args=array('term_args'=>array('child_of'=>$cat,'hide_empty'=>0,'orderby'=>'id'));
$terms = apply_filters( 'taxonomy-images-get-terms','',$args );
if ( ! empty( $terms ) ) {
echo '<ul>';
foreach ( (array) $terms as $term ) {
echo '<li><a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' ) . '</li>';
}
echo '</ul>';
}


User178986 comments:

Yes, that's the plugin I'm using!
Thank you very much, works like a charm!
Can you make it show the child-categories name below the thumbnail, please?
That would make it absolutely perfect!


Rempty comments:

try this

$cat = get_query_var('cat');
$args=array('term_args'=>array('child_of'=>$cat,'hide_empty'=>0,'orderby'=>'id'));
$terms = apply_filters( 'taxonomy-images-get-terms','',$args );
if ( ! empty( $terms ) ) {
echo '<ul>';
foreach ( (array) $terms as $term ) {
echo '<li><a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' ) . '
<br/>
'.$term->name.'
</li>';
}
echo '</ul>';
}


User178986 comments:

That's it!!!
Thank you very much & keep up the good work!

2017-06-14

Andrea P answers:

try this (look at the comments in the code for more info about how to print out details about the looped category):

<div class="entry-content">
<?php
$cat = get_query_var('cat');
$args = array(
'child_of' => $cat,
'hide_empty' => false,
'orderby' => 'id',
);

$cat_list = apply_filters( 'taxonomy-images-get-terms', '', array( 'term_args'=>$args ) );

if($cat_list) {
echo 'Child-Categories: <ul>';

foreach ( $cat_list as $child_cat ) {

/** Every $child_cat will be an object containing these properties:
* [term_id]
* [name]
* [slug]
* [term_group]
* [term_taxonomy_id]
* [taxonomy]
* [description]
* [parent]
* [count]
* [image_id]
*
* use them like $child_cat->name
*/
unset ( $img_url, $cat_link );

$img_url = wp_get_attachment_image_src( $child_cat->image_id, 'thumbnail' );
$cat_link = get_term_link($child_cat);

// print out the category with the image and the name, linking to its page
echo '<li><a href="'. esc_url($cat_link) .'"><img src="'. $img_url .'"/><span> '. $child_cat->name .'</span></a></li>' ;


}

echo '</ul>';
}
?>
</div><!-- .entry-content -->