WooCommerce how to check if page is_shop() in functions.php?

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.

2

4 Answers

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

1

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.

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

You can use function_exists

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

Official docs:

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like