- This topic has 11 replies, 3 voices, and was last updated 3 years, 2 months ago by
David.
-
AuthorPosts
-
February 10, 2023 at 12:12 am #2528124
Gary
Hi Guys
I am wanting to add a WooCommerce featured image as the background of a page header block element but can’t get it to work. I have previously done this with (with your help) using the theme page headers and it worked great but I can’t figure out how to do it with an element.
Any guidance on this would be massively appreciated.
Thanks in advance.
Cheers
Gary
February 10, 2023 at 12:16 am #2528127Fernando Customer Support
Hi Gary,
You can use a Block Element – Page Hero. Reference: https://docs.generatepress.com/article/block-element-page-hero/
This should work on WooCommerce Product pages as well.
February 12, 2023 at 3:49 am #2530160Gary
Hi Fernando
Sorry for the late reply, I didn’t receive an email to let me know.
This method does not appear to work with product categories, I had the same issue with the standard GeneratePress headers and you guys supplied me with some code for this. The problem is that the category image is not the featured image.
The code I was supplied for the standard GeneratePress headers is below:
add_filter( ‘generate_page_hero_background_image_url’, function( $url ) {
if ( ! function_exists( ‘get_woocommerce_term_meta’ ) ) {
return $url;
}global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, ‘thumbnail_id’, true );
$background_image = wp_get_attachment_url( $thumbnail_id );if ( $background_image ) {
$url = $background_image;
}return $url;
});Cheers
Gary
February 12, 2023 at 5:50 pm #2530851Fernando Customer Support
Hi Gary,
To clarify, are you wanting to add this on a Single Product Page or a WooCommerce Product Archive page/Shop Page?
February 12, 2023 at 6:32 pm #2530869Gary
Hi Fernando
This is for product/category pages.
Cheers
Gary
February 12, 2023 at 9:49 pm #2530955Fernando Customer Support
I see. What are you using currently, a Block Element or a Header Element?
February 13, 2023 at 12:05 am #2531020Gary
I am using a block element set to a page hero.
February 13, 2023 at 1:02 am #2531067Fernando Customer Support
I see, give the Container Block a class of
cu-container-bg.Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/
Then, add this snippet:
add_filter('generateblocks_background_image_url', function($url, $attributes){ if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-container-bg' ) !== false && wp_is_mobile() ) { if ( ! function_exists( ‘get_woocommerce_term_meta’ ) ) { return $url; } global $wp_query; $cat = $wp_query->get_queried_object(); $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, ‘thumbnail_id’, true ); $background_image = wp_get_attachment_url( $thumbnail_id ); if ( $background_image ) { $url = $background_image; } } return $url; }, 10, 2);Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
February 13, 2023 at 2:29 am #2531149Gary
Still not working for me mate, what block settings do you have?
February 13, 2023 at 7:20 am #2531489David
StaffCustomer SupportHi there,
remove the snippet that Fernando provided.
And add this:
// Container Background image Product Category add_filter( 'generateblocks_background_image_url', function( $url, $settings ) { // Get the category thumbnail global $wp_query; $cat = $wp_query->get_queried_object(); $thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true ); $image_url = wp_get_attachment_url( $thumbnail_id ); if ( $image_url && ! empty( $settings['className'] ) && strpos( $settings['className'], 'cat-image' ) !== false ){ return $image_url; } return $url; },10 ,2 );Then select the Container block and in Advanced > Additional CSS classes add:
cat-imageFebruary 13, 2023 at 7:25 am #2531497Gary
Success!! Thanks David that’s great mate. Much appreciated!!
February 13, 2023 at 7:28 am #2531501David
StaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.