skip to Main Content

I’m new here.

I have installed a sidebar on my Woocommerce shop page like so (in a woocommerce.php file):

<main role="main" class="container row">
    <div class="col-md-2 filtersmargin">
        <?php
        if (is_active_sidebar('sidebar')) {
            dynamic_sidebar('sidebar');
        }
        ?> 
    </div>
    <div class="col-md-10">
        <div class="woocommerce">
            <?php woocommerce_content(); ?>
        </div>
    </main><!-- /.container -->

However, this sidebar is of course also displaying on single product pages. The problem is just that the filters in my sidebar are irrelevant when viewing a single product (size, color, etc.) and therefore I would like to remove it from there. I have done so succesfully in the past with snippets from Google (when I did not create my own custom theme but used Astra), but now I cannot make it work anymore. I suspect it is due to the way that I am integrating the sidebar in the first place, but I am not sure.

If anyone have any ideas as to how I can remove it only on the single product pages, that would be very much appreciated.

2

Answers


  1. You’ll want to use a conditional statement checking what kind of page you are in.

    You can find more functions and details here

    The code might look something like

    <main role="main" class="container row">
    <?php if (! is_product() ){  //if not single product page ?>
    <div class="col-md-2 filtersmargin">
        <?php
        if (is_active_sidebar('sidebar')) {
            dynamic_sidebar('sidebar');
        }
        ?> 
    </div>
    <div class="col-md-10">
    <?php }else{ // this depends on how you want your template to look ?>
      <div class="col-md-12">
    <?php } ?>
        <div class="woocommerce">
            <?php woocommerce_content(); ?>
        </div>
    
    </main><!-- /.container -->
    

    If you want that column to just be empty on single product page you can just change the one line:

    if ( is_active_sidebar('sidebar') && ! is_product() ) {
    

    I’m not sure which template file you are editing so it might be worth while looking into that.

    Side note:If you are editing the files inside the WooCommerce plugin, remember it’s better to copy template files into your theme/child-theme and override them that way so you can update your plugin without loosing changes to the templates.

    Login or Signup to reply.
  2. a way is to check if it is single product by is_product() or is_singular('product') functions and if it is not show sidebar.

    an other way is to completely copy template folder from woocommerce plugins root, and paste it into themes/current-theme/woocommerce and then customize single-product.php file.

    note: you don’t need to copy all files. you can only copy files that you need to customize (here is single-product.php).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search