24th Jul '24
/
0 comments

[WooCommerce] Product Category Image Background in Bricks

[WooCommerce] Product Category Image Background in Bricks

Looking to set up the product category thumbnail image as the background of a Section in Bricks?

Here’s how.

Step 1

Edit your product categories in the WP admin and set images for each.

Step 2

Register a custom function (to be used on WooCommerce term archives) that returns the URL of the product category image.

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php 

function bl_get_wc_category_image_url(): string {
    // Check if WooCommerce is active
    if ( ! function_exists( 'is_woocommerce' ) ) {
        return false;
    }

    // Get the category thumbnail ID
    $thumbnail_id = get_term_meta( get_queried_object_id(), 'thumbnail_id', true );

    // If there's no thumbnail ID, return false
    if ( ! $thumbnail_id ) {
        return false;
    }

    // Get the image URL
    $image_url = wp_get_attachment_url( $thumbnail_id );

    // Return the image URL
    return $image_url;
}

Step 3

Whitelist the bl_get_wc_category_image_url function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_get_wc_category_image_url'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides bl_get_wc_category_image_url. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

Step 4

Create a Bricks template for product category archives and edit it with Bricks.

Add a Section and inside its Container, a h1 Heading for showing the product category name.

{term_name}

Select the Section → STYLE → BACKGROUND.

Paste this dynamic data tag:

{echo:bl_get_wc_category_image_url}

You could optionally set a gradient color as the overlay.

Get access to all 633 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

How To Remove the Featured Image from WooCommerce Product Gallery

How To Remove the Featured Image from WooCommerce Product Gallery

Product Gallery on single product pages in WordPress include the product's featured image by default. If you are looking to exclude it so only the…
Categories:
Is WooCommerce Cart Empty Condition in Bricks

Is WooCommerce Cart Empty Condition in Bricks

Updated on 5 Nov 2024 Looking to conditionally render an element depending on whether the user's cart is empty or not when using WooCommerce? Add…
Pro
Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

On product pages, we show all the FAQs associated with the current WooCommerce product's product categories.
Categories:
Side-by-side WooCommerce Quantity and Add to Cart in Bricks

Side-by-side WooCommerce Quantity and Add to Cart in Bricks

Looking to position the quantity input field and Add to cart button next to each other instead of one below the other in Bricks? Add…
Categories:
Pro
On Sale Products Bricks Query Loop

On Sale Products Bricks Query Loop

Looking to show only the WooCommerce products that are on sale in Bricks? Set up a query loop, enable the PHP query editor and paste:
Categories:
Pro
Stock Quantity Block for WP Grid Builder

Stock Quantity Block for WP Grid Builder

In the unofficial WP Grid Builder Facebook group a user asked: Is it possible to show stock quantity on the card? I can't find it…
Categories: