16

In WooCommerce, My Category Listing page and product listing page are rendered from archieve-product.php ( By Default) . How to check if page is_shop() in functions.php? As is_shop function does not work in functions.php. I simply want to remove my sidebar from Category listing page not from product listing page.

kd patel
  • 607
  • 1
  • 6
  • 15
  • 1
    What is your question? Using the function is fairly straight forward: *"Returns true when on the product archive page (shop)"*. – George May 20 '16 at 15:52
  • Yes .. But the problem is that When I click on particular category. The products are displayed of that categories which is also being rendered from the same archieve-product.php. So the side bar get dis appear even in product listing page. ... Does this make any sense :/ ?? – kd patel May 20 '16 at 16:07

4 Answers4

21

When placed inside a hook, is_shop will work in functions.php

add_action( 'template_redirect', 'custom_template_redirect' );

function custom_template_redirect() {

    if( is_shop() ) :

         // code logic here

    endif;    
}

Here is a list of all WooCommerce conditionals

Anand Shah
  • 14,575
  • 16
  • 72
  • 110
5

You can write a condition into "archive-product.php" for category page like,

    $cate = get_queried_object();
    if(is_product_category()  && $cate->parent != 0 ){

         // Write code here
         //include sidebar here
    }

By using this code this will check the page for product_category and also check for a parent.

jchuck
  • 55
  • 7
Mukesh Ram
  • 6,248
  • 4
  • 19
  • 37
3

call it using WordPress Hook pre get posts

add_action('pre_get_posts','nameTheFunction');

function nameTheFunction(){

   if(is_shop()){

    // your code here 

  }

}// function end here

Read more about pre get posts Hook

https://developer.wordpress.org/reference/hooks/pre_get_posts/

Ibrahim Hajjaj
  • 132
  • 1
  • 5
1

You can use function_exists

if( function_exists("is_shop") ) {
    // call it or do something else
}
else {
    // load it from somewhere
}

Official docs: https://secure.php.net/function_exists

Cfreak
  • 19,191
  • 6
  • 49
  • 60
  • 1
    The function is "is_shop", so your check would be `function_exists('is_shop')`. – George May 20 '16 at 15:53
  • Cool thats working for me now :) Well but still I have a problem. My category listing page and product listing page are rendering from same page "archieve-product.php" . i just want to remove side bar from category listing page . can i do it from functions.php? Currently it got removed from both the pages with the above code. – kd patel May 20 '16 at 15:55
  • @kdpatel you should post some code (perhaps in another question) that gives some more detail about what is wrong – Cfreak May 20 '16 at 15:59
  • `add_action('init', 'remove_sidebar_woo'); function remove_sidebar_woo(){ if( function_exists("is_shop") ) { // call it or do something else remove_action('woocommerce_sidebar', 'woocommerce_get_sidebar', 10); } else { // load it from somewhere } } ` – kd patel May 20 '16 at 16:00
  • I think you want to call `is_shop()` . My function just determines whether or not it exists. You really should edit the original post instead of posting the code in comments. – Cfreak May 20 '16 at 16:04