Sidebar only on WooCommerce shop pages

Hey there again,

I’ve kinda run into a problem again and can’t really find a solution.
There is this site (I’ll provide the data in the secure note) and it as a few pages, one of them being the shop page. I want a sidebar to only appear on the shop page to display the different product categories. All other pages shouldn’t have sidebars, since nobody anyways really cares about sidebars anymore.

I’ve read the post where someone didn’t want the sidebar to appear in it’s blog, and I get the css class selectors but in this case, it’s a specific page that only should have a sidebar. And since all pages kinda have the .x-main class I can’t quite figure it out. Or is there a way to solve it in the functions.php of the child-theme?

Happy for all the help as always.

Marc

Hello Marc,

Thanks for writing in!

Is your global content layout set as fullwidth? To force a sidebar in the shop page and the WooCommerce archive pages, since the child theme is set up, please add the following code in your child theme’s functions.php file

// Sidebars in shop page and woo archive pages
// =============================================================================
function add_sidebars_woopages($contents) {
  if ( x_is_shop() || x_is_product_category() || is_product_tag() ) {
    $contents = 'content-sidebar';
  }
  
  return $contents;
}
add_filter('x_option_x_layout_content', 'add_sidebars_woopages');
// =============================================================================

We would loved to know if this has work for you. Thank you.

Hello RueNel,

thanks for the fast answer and sorry for just trying this out now.

I’ve pasted the code in the Child Theme but I always get the following error message:

Your PHP code changes were rolled back due to an error on line 29 of file wp-content/themes/x-child/functions.php. Please fix and try saving again.

Uncaught Error: Call to undefined function x_is_shop() in wp-content/themes/x-child/functions.php:29
Stack trace:
#0 wp-includes/class-wp-hook.php(286): add_sidebars_woopages('sidebar-content')
#1 wp-includes/plugin.php(203): WP_Hook->apply_filters('sidebar-content', Array)
#2 wp-content/themes/x/framework/functions/helpers.php(48): apply_filters('x_option_x_layo...', 'sidebar-content')
#3 wp-content/themes/x/framework/functions/thumbnails.php(78): x_get_option('x_layout_conten...')
#4 wp-content/themes/x/functions.php(226): x_post_thumbnail_width()
#5 wp-settings.php(443): include('/var/www/vhosts...')
#6 wp-config.php(77): require_once('/var/www/vhosts...')
#7 /var/www/

Normally it’s set as fullwidth because I don’t want to have any sidebars at all on the page except for on the shop page. So for this I’ve tried setting it to “sidebar left, content right” but it still gives the same error.

Hi There,

Please change the previous code to this:

function add_sidebars_woopages($contents) {
  if ( is_shop() || is_product_category() || is_product_tag() ) {
    $contents = 'content-sidebar';
  }
  
  return $contents;
}
add_filter('x_option_x_layout_content', 'add_sidebars_woopages');

Hope it helps :slight_smile:

1 Like

Amazing. Thank you, that worked just perfectly fine.
Have a wonderful day.

You’re more than welcome, glad we could help.

Cheers!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.