• This is a solution for those who need to add image for Shop or Category

    
    /**
     * Display category image on category archive
     */
    add_action('woocommerce_archive_description', 'woocommerce_category_image', 2);
    function woocommerce_category_image()
    {
        if (is_product_category()) {
            global $wp_query;
            $cat = $wp_query->get_queried_object();
            $thumbnail_id = get_term_meta($cat->term_id, 'thumbnail_id', true);
            $image = wp_get_attachment_url($thumbnail_id);
            if ($image) {
                echo '<div style="margin-bottom: 20px;">';
                echo '<img src="' . $image . '" />name . '"/>';
                echo '</div>';
            }
        }
    }
    
    
    /**
     * Display shop image on archive
     */
    add_action('woocommerce_archive_description', 'woocommerce_shop_feature_image', 2);
    function woocommerce_shop_feature_image()
    {
        if (is_shop()) {
            echo '<div style="margin-bottom: 20px;">';
            echo get_the_post_thumbnail(get_option('woocommerce_shop_page_id'));
            echo '</div>';
        }
    }
    
Viewing 7 replies - 1 through 7 (of 7 total)
  • Thank you for your code.
    I want to show the category image on each category page.
    I don’t know which theme you use but for me it doesn’t work. I use DIVI and it doesn’t work. For some reason, it’s possible that Divi doesn’t show the image in the template.
    Patrick

    Hi,
    I’ve fixed my problem with DIVI theme.
    Cheers

    littleblackcatjewellery

    (@littleblackcatjewellery)

    How would I use this please? Not a coder here

    @littleblackcatjewellery add that code in “appearance > theme editor > functions.php”

    Pat

    (@patrickhaond)

    Hi there.
    There is a code error in the function to Display category image on category archive ?

    Hi there,

    I am still pretty new at this but the solution below worked for me.
    Hope this helps.

    `add_action(‘woocommerce_archive_description’, ‘woocommerce_category_image’, 2);
    function woocommerce_category_image()
    {
    if (is_product_category()) {
    global $wp_query;
    $cat = $wp_query->get_queried_object();
    $thumbnail_id = get_term_meta($cat->term_id, ‘thumbnail_id’, true);
    $image = wp_get_attachment_url($thumbnail_id);
    if ($image) {
    echo ‘<img src=”‘;
    echo $image;
    echo ‘”/>’;
    }
    }
    }

    tawabwp

    (@tawabwp)

    I have somewhat edited your code. This should work:

    /**
     * Display category image on category archive
     */
    add_action('woocommerce_archive_description', 'woocommerce_category_image', 2);
    function woocommerce_category_image(){
        if (is_product_category()) {
            global $wp_query;
            $cat = $wp_query->get_queried_object();
            $thumbnail_id = get_term_meta($cat->term_id, 'thumbnail_id', true);
            $image = wp_get_attachment_url($thumbnail_id);
    		if ($image){
    			echo '<img src="'.$image.'" alt="'.$cat->name.'"/>';
    		}
        }
    }
    • This reply was modified 4 years ago by tawabwp.
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display category image and featured image on Archive’ is closed to new replies.