Search code examples
wordpresswoocommerceattributespermalinks

Show content before main content based on color attribute


I found a code snippet to display the content before the main content and it worked.

Currently the content is displayed on all pages. (except shop page)

The code :

add_action( 'woocommerce_before_main_content', 'BannerShop', 35 );

function BannerShop(){

if(!is_shop()){
echo '<img src="https://localhost/demosite/wp-content/uploads/2015/06/512x356.png" >';
     }
  }

What I want to ask is, how to display content only for color attribute products in the form of links.

Example :

The display (content) will ONLY SHOW when the url is like this :

mysite.com/color/red/

Sorry if the explanation is not good because I don't really understand this.

Any help is greatly appreciated.

thank you


Solution

  • I understand your question is about displaying that extra content, if the current query is for a product archive page only showing products of a certain attribute 'color'.

    Each WooCommerce attribute is an independent taxonomy.

    WordPress's is_tax('parameter') function checks, if the query is for an existing custom taxonomy archive page (other than category & tag) & if the query is for that specific taxonomy 'parameter', in your case 'color'.

    So, this code snippet in your functions.php or equivalent plugin should work:

    add_action( 'woocommerce_before_main_content', 'BannerShop', 35 );
    
    function BannerShop(){
        (is_tax('color')) {
            echo '<img src="https://localhost/demosite/wp-content/uploads/2015/06/512x356.png" >';
        }
    }
    

    Though, to make the above template WooCommerce override work, declare WooCommerce support for your theme by adding the following lines to your functions.php or plugin equivalent:

    function theme_add_woocommerce_support() {
        add_theme_support( 'woocommerce' );
    }
    
    add_action( 'after_setup_theme', 'theme_add_woocommerce_support' );