I'm trying to write a php code on my index file in wordpress. I have to get the images & names of each category, this is what I've got so far:
$term = get_queried_object()->term_id;
$termid = get_term($term, 'product_cat' );
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'child_of' => $term,
'parent' =>0
);
$subproducts = get_terms( 'product_cat', $args);
foreach ($subproducts as $subproduct) {
echo $subproduct->name.'<br>';
echo $subproduct->term_id; //get the category Id
}
How do I get the category image url?
Change your code to this
foreach ($subproducts as $subproduct): setup_postdata($subproduct);
the_title();
the_ID();
the_post_thumbnail();
endforeach;
You need to use get_woocoomerce_term_meta
and get the thumbnail_id
from it, changing the loop as below will get the job done
foreach( $subproducts as $subproduct ) {
echo $subproduct->name.'<br>';
echo $subproduct->term_id; //get the category Id
$thumbnail_id = get_woocommerce_term_meta( $subproduct->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<img src="'.$image.'" alt="" width="762" height="365" />';
}