How to Display the WooCommerce Category Image?

11 minutes read

To display the WooCommerce category image, you can follow these steps:

  1. First, ensure that you have set a thumbnail image for your WooCommerce product category. This can be done by navigating to WooCommerce > Categories in your WordPress admin dashboard. Edit the desired category and upload/select an image for the category thumbnail.
  2. Now, open the template file where you want to display the category image. This can vary depending on your theme, but generally, it could be archive-product.php or taxonomy-product_cat.php.
  3. Locate the code where the category title or loop is being displayed. You can find it typically within a loop like "while ( have_posts() ) : the_post();".
  4. Within the loop, you can use the following code to display the category image:
1
2
3
4
5
6
7
8
<?php
    $category = get_queried_object();
    $thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
    $image = wp_get_attachment_url( $thumbnail_id );
    if ( $image ) {
        echo '<img src="' . $image . '" alt="' . $category->name . '" />';
    }
?>


The code retrieves the category object, extracts the thumbnail ID, and retrieves the image URL. It then checks if an image exists and if so, displays it along with the category name as an alt attribute.

  1. Save the file and visit your category page to see the category image displayed alongside the title or loop.


These steps should help you display the WooCommerce category image on your website.

Best WooCommerce Hosting Providers in 2024

1
DigitalOcean

Rating is 5 out of 5

DigitalOcean

2
AWS

Rating is 4.9 out of 5

AWS

3
Vultr

Rating is 4.8 out of 5

Vultr

4
Cloudways

Rating is 4.7 out of 5

Cloudways


How can I make the WooCommerce category image clickable?

To make the WooCommerce category image clickable, you can follow these steps:

  1. Locate the archive-product.php file in your WooCommerce theme folder. If it doesn't exist, you can create it by copying the archive-product.php file from the WooCommerce plugin directory to your theme directory.
  2. Open the archive-product.php file in a text editor.
  3. Search for the line of code that displays the category image. It may look similar to this: woocommerce_subcategory_thumbnail().
  4. Wrap that line of code with an anchor () tag. For example:
1
2
3
<a href="<?php echo get_term_link( $category->slug, 'product_cat' ); ?>">
    <?php woocommerce_subcategory_thumbnail( $category ); ?>
</a>


  1. Save the changes to the archive-product.php file.
  2. Now, when you visit the WooCommerce shop page or category pages, the category image should be clickable and redirect to the specific category page.


Note: Modifying theme files directly is not recommended as it may get overwritten during theme updates. To maintain the modifications, consider creating a child theme and making the changes in the child theme's archive-product.php file.


What are the steps to display the category image on the WooCommerce shop page?

To display the category image on the WooCommerce shop page, you can follow these steps:

  1. Open your theme's functions.php file (you can find it in your theme's directory).
  2. Add the following code to the functions.php file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/**
 * Add Category Image to WooCommerce Shop Page
 */
function display_category_image_on_shop_page() {
    // Get the current category
    $category = get_queried_object();

    // Check if the category has an image
    if ( ! empty( $category->image ) ) {
        // Output the category image
        echo wp_get_attachment_image( $category->image, 'full' );
    }
}
add_action( 'woocommerce_before_shop_loop', 'display_category_image_on_shop_page', 5 );


  1. Save the functions.php file.


Now, when you visit the WooCommerce shop page, the category image will be displayed before the product listings.


What is the purpose of the WooCommerce category image?

The purpose of the WooCommerce category image is to visually represent and differentiate product categories within an online store. It is an image that is associated with a specific product category, helping customers quickly identify and navigate to the desired category while browsing through the store. The category image enhances the user experience by providing a visual cue and making it easier for customers to locate the products they are interested in.

Best WooCommerce Books to Read in 2024

1
Building Your Online Store With WordPress and WooCommerce: Learn to Leverage the Critical Role E-commerce Plays in Today’s Competitive Marketplace

Rating is 5 out of 5

Building Your Online Store With WordPress and WooCommerce: Learn to Leverage the Critical Role E-commerce Plays in Today’s Competitive Marketplace

2
Mastering WooCommerce 4: Build complete e-commerce websites with WordPress and WooCommerce from scratch

Rating is 4.9 out of 5

Mastering WooCommerce 4: Build complete e-commerce websites with WordPress and WooCommerce from scratch

3
WooCommerce Cookbook

Rating is 4.8 out of 5

WooCommerce Cookbook

4
Building E-Commerce Solutions with WooCommerce - Second Edition

Rating is 4.7 out of 5

Building E-Commerce Solutions with WooCommerce - Second Edition

5
Mastering WooCommerce: Build, Customize, and Optimize Your Complete E-commerce Website with WooCommerce from scratch, 2nd Edition

Rating is 4.6 out of 5

Mastering WooCommerce: Build, Customize, and Optimize Your Complete E-commerce Website with WooCommerce from scratch, 2nd Edition


Can I display the WooCommerce category image on the single product page?

Yes, you can display the WooCommerce category image on the single product page by adding some code to your theme's functions.php file or by using a custom plugin. Here is an example code snippet that you can use:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// Add category image to single product page
add_action('woocommerce_before_single_product_summary', 'display_category_image', 5 );
function display_category_image() {
    global $product;
    
    // Get the product categories
    $categories = wp_get_post_terms( $product->get_id(), 'product_cat' );
    
    // Loop through each category
    foreach($categories as $category) {
        $cat_id = $category->term_id;
        $thumbnail_id = get_woocommerce_term_meta( $cat_id, 'thumbnail_id', true );
        $image = wp_get_attachment_url( $thumbnail_id );
        
        // Display category image
        if ($image) {
            echo '<img src="' . $image . '" alt="' . $category->name . '" />';
        }
    }
}


Add this code to your theme's functions.php file or your custom plugin, and the category image will be displayed on the single product page.


Please note that you might need to adjust the CSS styles according to your theme's design to ensure proper alignment and display of the category image.


What are the available hooks to modify the WooCommerce category image display?

The following are the available hooks to modify the WooCommerce category image display:

  1. woocommerce_before_subcategory_title: This hook allows you to modify the display of the category image before the category title.
  2. woocommerce_before_subcategory: This hook allows you to modify the display of the entire category grid item.
  3. woocommerce_before_subcategory_object: This hook allows you to modify the display of the category image as an object.
  4. woocommerce_after_subcategory_title: This hook allows you to modify the display of the category image after the category title.
  5. woocommerce_shop_loop_subcategory_title: This hook allows you to modify the display of the category image within the shop/category loop.


You can use these hooks in your theme's functions.php file or in a custom plugin to modify the display of the WooCommerce category images according to your requirements.


What is the recommended image format for the WooCommerce category image?

The recommended image format for the WooCommerce category image is a square image in JPG or PNG format. The image should have a resolution of at least 300x300 pixels to ensure a clear and sharp display on your online store.


How to display the WooCommerce category image above the category title?

To display the WooCommerce category image above the category title, you can follow these steps:

  1. Open your theme's functions.php file.
  2. Add the following code at the end of the file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
add_action( 'woocommerce_before_subcategory_title', 'display_category_image', 10 );

function display_category_image() {
    global $wp_query;

    $term = $wp_query->get_queried_object();
    $thumbnail_id = get_term_meta( $term->term_id, 'thumbnail_id', true );

    if ( $thumbnail_id ) {
        echo wp_get_attachment_image( $thumbnail_id, 'medium' );
    }
}


  1. Save the file and open the category archive template file (archive-product.php or taxonomy-product_cat.php) in your theme directory.
  2. Find the line of code that displays the category title and move it below the woocommerce_before_subcategory_title hook. For example, you may have a line similar to this:
1
<h2><?php woocommerce_page_title(); ?></h2>


Change it to:

1
2
<?php do_action( 'woocommerce_before_subcategory_title' ); ?>
<h2><?php woocommerce_page_title(); ?></h2>


  1. Save the file, and the category image should now be displayed above the category title on your WooCommerce category pages.


Note: This method assumes that you have assigned a featured image for each category in the WooCommerce category settings.

Facebook Twitter LinkedIn Telegram

Related Posts:

To show a category of WooCommerce products, you can navigate to the WordPress admin dashboard and go to the WooCommerce &gt; Products section. From there, click on the Categories tab and select the category you want to display. Once you have chosen the categor...
To display an image in an Oracle form, you can use a graphical image item. First, you need to create a graphical image item in the form. Then, set the image item&#39;s properties such as Filename and Image Format to specify the image you want to display. You c...
To get all products from a specific category in WooCommerce, you can use the built-in functions and filters provided by WooCommerce. One way to do this is by using the get_posts function along with the product_cat parameter to fetch products from a specific ca...