Tuesday, February 19, 2019

How to display Woocommerce Category image in wordpress | Wordpress.

To display the category image for the currently displayed category in archive-product.php, use the current category term_id when is_product_category() is true:
// verify that this is a product category page
 
if ( is_product_category() ){
    global $wp_query;

    // get the query object
    $cat = $wp_query->get_queried_object();

    // get the thumbnail id using the queried category term_id
    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ); 

    // get the image URL
    $image = wp_get_attachment_url( $thumbnail_id ); 

    // print the IMG HTML
    echo "<img src='{$image}' alt='' width='762' height='365' />";
}
 FOR EXAMPLE: 
 $taxonomyName = "product_cat";
$parent_terms = get_terms($taxonomyName, array('orderby' => 'include','include' =>
array(22,51,18,19,21,20)));
foreach ($parent_terms as $pterm) {
$thumbnail_id = get_woocommerce_term_meta($pterm->term_id, 'thumbnail_id', true);
// get the image URL for parent category
$image = wp_get_attachment_url($thumbnail_id); 
//show parent categories
echo '<div class="col-md-2">
<div class="item-product-cat">
<div class="slider-item-image">
<a href="' . get_term_link($pterm->name, $taxonomyName) . '" 
title="Body Parts"><img src="'.$image.'" alt="' . $pterm->name . '"></a>
     </div>
 <div class="item-content">
<h3><a href="' . get_term_link($pterm->name, $taxonomyName) . '">' . $pterm->name . '</a></h3>
     </div>
     </div>
     </div>';
 }
    

No comments:

Post a Comment