Woocommerce conditional tags for custom sidebar is twentyseventeen theme

Issue

I’m using twentyseventeen theme and Woocommerce and want to have a custom sidebar for my woo commerce pages rather than the default theme sidebar (sidebar-1) which will be used elsewhere on the site.

So in my child theme’s functions.php I registered my custom sidebar (sidebar-6), populated it with widgets and tried to call it with a woo commerce conditional tag in my child theme’s sidebar.php

I’m new to conditional tags and despite following advice on various online sites, so far have not been able to get this to work. I’d be very grateful if someone could point me in the right direction with this.

The original theme sidebar.php looks like this

if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>

<aside id="secondary" class="widget-area" role="complementary" 
aria-label="<?php esc_attr_e( 'Blog Sidebar', 'twentyseventeen' ); 
?>">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->

I have tried various combinations of code to try to make this work but so far have failed. This is what I currently have and it results in no sidebar showing up on any pages.

if ( is_woocommerce() ) : {
return;
}
?>

<aside id="secondary" class="widget-area" role="complementary" 
aria-label="<?php esc_attr_e( 'Shop Sidebar', 'twentyseventeen' );    
?>">
<?php dynamic_sidebar( 'sidebar-6' ); ?>
</aside><!-- #secondary -->


<?php else:
return;
}

?>
<aside id="secondary" class="widget-area" role="complementary" 
aria-label="<?php esc_attr_e( 'Blog Sidebar','twentyseventeen');         
?>">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->
<?php endif ?>

Solution

you have a mistake in the following condition:

if ( is_woocommerce() ) : {
return;
  }

basically above you are saying if it’s Woocommerce pages return and do nothing.

here is the correct one:

if ( is_woocommerce() && is_active_sidebar( 'sidebar-6' ) ) :?>
<aside id="secondary" class="widget-area" role="complementary" 
aria-label="
    <?php esc_attr_e( 'Shop Sidebar', 'twentyseventeen' ); ?>">
    <?php dynamic_sidebar( 'sidebar-2' ); ?>
</aside><!-- #secondary -->
    <?php
    elseif ( is_active_sidebar( 'sidebar-1' ) ) :
        ?>
<aside id="secondary" class="widget-area" role="complementary" 
aria-label="<?php esc_attr_e( 'Blog Sidebar', 'twentyseventeen' ); ?>">
        <?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->
<?php endif; ?>

Answered By – kashalo

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published