Skip to content
Advertisement

How to use WooCommerce is_shop conditional tag excluding searchs results

I have a function that uses the conditional tag is_shop.

The function is intended to display a widget on the shop page only. However, it also returns the widget when a search is performed.

Is there a way to only return it on the shop main page.

My code is:

function cs_beforeproducts_sidebar() {
    if( is_shop() ){
        get_sidebar( 'aboveproducts' );
    } else {
}
}

I’ve tried using is_page as well with no joy.

Advertisement

Answer

You can use WordPress is_search() as a conditional tag like:

function cs_beforeproducts_sidebar() {
    // Only shop and not a search
    if( is_shop() && ! is_search() ){
        get_sidebar( 'aboveproducts' );
    } else {
        // something else
    }
}

Or as a product search adds ‘s’ query variable in URL, you can try to use:

function cs_beforeproducts_sidebar() {
    // Only shop and not search
    if( is_shop() && ! isset($_GET['s']) ){
        get_sidebar( 'aboveproducts' );
    } else {
        // something else
    }
}

Both works.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement