In a WooCommerce store, displaying banners on category pages can help draw attention to specific product categories, promote sales or discounts, or create a visually appealing customer browsing experience. In this guide, we’ll show you how to showcase a text banner on a category page for a specific category, allowing you to highlight key categories and capture your customers’ interest.
/**
* Snippet Name: Show a text banner on specific category page
* Snippet Author: wpsnippets.dev
*/
//Display text banner on category page for a specific category
function wps_display_category_banner() {
if ( is_product_category( 'albums' ) ) {
echo '<div class="wps-category-banner">Your custom text or HTML goes here</div>';
}
}
add_action( 'woocommerce_before_main_content', 'wps_display_category_banner', 10 );
Code Explanation
- This code snippet defines a function wps_
display_category_banner
that checks if the current page is a category page for a specific category using theis_product_category
function. - If the condition is met, it displays the HTML markup for the category banner containing your custom text or HTML content.
- The function is hooked into the
woocommerce_before_main_content
action hook to ensure that the banner is displayed at the top of the category page content.