Multiple Sidebars

Hello -

I’ve used code that I found on another forum support entry (though I’m not sure which one at this point) to successfully add sidebars to all my shop pages by modifying the functions.php file in my child theme. However, it is adding my main sidebar to the pages and I have a second custom sidebar that I use on my shop pages. How do I make it call the correct sidebar (my shop sidebar)?

Thanks - Erin

// Add Sidebar to All Shop Pages
// =============================================================================

function x_get_content_layout() {

$stack          = x_get_stack();
$content_layout = x_get_option( 'x_' . $stack . '_layout_content', 'content-sidebar' );

if ( $content_layout != 'full-width' ) {
  if ( is_home() ) {
    $opt    = x_get_option( 'x_blog_layout', 'sidebar' );
    $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
  } elseif ( is_singular( 'post' ) ) {
    $meta   = get_post_meta( get_the_ID(), '_x_post_layout', true );
    $layout = ( $meta == 'on' ) ? 'full-width' : $content_layout;
  } elseif ( x_is_portfolio_item() ) {
    $layout = 'full-width';
  } elseif ( x_is_portfolio() ) {
    $meta   = get_post_meta( get_the_ID(), '_x_portfolio_layout', true );
    $layout = ( $meta == 'sidebar' ) ? $content_layout : $meta;
  } elseif ( is_archive() ) {
    if ( x_is_shop() || x_is_product_category() || x_is_product_tag() || x_is_product() ) {
      $layout = 'content-sidebar';
    } else {
      $opt    = x_get_option( 'x_archive_layout', 'sidebar' );
      $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
    }
  } elseif ( x_is_product() ) {
    $layout = 'content-sidebar';
  } elseif ( x_is_bbpress() ) {
    $opt    = x_get_option( 'x_bbpress_layout_content', 'sidebar' );
    $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
  } elseif ( x_is_buddypress() ) {
    $opt    = x_get_option( 'x_buddypress_layout_content', 'sidebar' );
    $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
  } elseif ( is_404() ) {
    $layout = 'full-width';
  } else {
    $layout = $content_layout;
  }
} else {
  $layout = $content_layout;
}

  if ( is_page_template( 'template-layout-content-sidebar.php' ) ) {
    $layout = 'content-sidebar';
  } elseif ( is_page_template( 'template-layout-sidebar-content.php' ) ) {
    $layout = 'content-sidebar';
  } elseif ( is_page_template( 'template-layout-full-width.php' ) ) {
    $layout = 'full-width';
  }

return $layout;

}

add_filter( ‘ups_sidebar’, function ( $default_sidebar ) {

$sidebars = get_option( ‘ups_sidebars’ );

foreach ( $sidebars as $id => $sidebar ) {
if ( x_is_shop() || x_is_product_category() || x_is_product_tag() || x_is_product() ) {
if ( array_key_exists( ‘index-shop’, $sidebar ) && $sidebar[‘index-shop’] == ‘on’ ) {
return $id;
}
}
}

return $default_sidebar;

}, 9999 );

Hello There,

Thanks for writing in! The main sidebar will always be added to the page and posts. Unless you have created another sidebar and assigned it to a page or your posts. In the code above, index-shop is the name of the sidebar you may have assign for the shop page. In the condition of the code above, the index-shop sidebar will be displayed in the shop page, product category archive, product tag archive and in each of the product pages.

Have you created a custom sidebar in Appearance > Sidebars already? Perhaps you must create a sidebar first and assign it as your shop page sidebar or enable it to be displayed in the shop page.

And with the help of the code above, this sidebar should be displayed in all those conditions mentioned in the code.

Please let us know how it goes.

I’m sorry, I’m afraid I wasn’t clear enough in my initial comment.

I have already created a custom sidebar (shop_sidebar) and it shows in my /Shop page along with my /product-category and individual /product pages. However, I would like for it to also show on my /cart, /checkout, and /wishlist pages. The custom code successfully made a sidebar show on those pages but it is showing my main sidebar and not the custom shop_sidebar. I would like to make my custom shop_sidebar show on the /cart, /checkout, and /wishlist pages.

Thanks -Erin

The conditional tag for cart and checkout pages is is_woocommerce. See https://docs.woocommerce.com/document/conditional-tags/ for more details. You can add it along with these conditional tags in your code x_is_shop() || x_is_product_category() || x_is_product_tag() || x_is_product(). I’m not sure what powers your wishlist page, if you’re using a plugin, contact the plugin developer to know what conditional tag to use. If not, please see this list of conditional tags and use one that is right for your page.

If that doesn’t work, please consult with a developer as this would be getting into custom development. We do have an in-house custom development team that offer paid services, who may be able to assist. They can be contacted at [email protected] if this is of interest to you.

Thank you for your understanding.