Site logo

Archived topic

Hide a category from the Shop page

6 replies · Started by ilie on April 19, 2021

Viewing posts 1–7 of 7

With my previous theme, I use this code snippet to hide the "uncategorized" category from the main shop page. However, with GeneratePress, it does not matter what I return as the result of the get_terms filter, all the categories are always displayed. I am at a loss on how to hide the category. This works as expected with the default WordPress theme TwentyTwenty one, and other themes.

<?PHP
//code in functions.php

add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
    $new_terms = array();
    // if a product category and on the shop page
    // to hide from shop page, replace is_page('YOUR_PAGE_SLUG') with is_shop()
    if ( in_array( 'product_cat', $taxonomies ) && is_shop() ) {
        foreach ( $terms as $key => $term ) {
            if ( ! in_array( $term->slug, array( 'uncategorized' ) ) ) {
                $new_terms[] = $term;
            }
        }
        $terms = $new_terms;
    }
    return $terms;
}

Hi there,

Can you tell us how you're adding the code?

Perhaps you've added it to the functions.php of twenty twenty one?

I suggest using a Code Snippets plugin to avoid this potential confusion. Code snippet plugin is a good alternative to functions.php. It's not theme dependent.

Hello,

I neglected to say this the first time, but GeneratePress is a breath of fresh air in the WordPress themes. As a developer is a delight to work with it. And I am sure this issue will get sorted.

Back to the matter at hand:

I have added it to GP Child theme functions.php, along with other code. There, I also have debug statements that show the code is being executed. It is almost like GP is doing it's own display of the categories, that will not call the same hooks as the default WooCommerce templates?

I have added it to GP Child theme functions.php, along with other code. There, I also have debug statements that show the code is being executed. It is almost like GP is doing it’s own display of the categories, that will not call the same hooks as the default WooCommerce templates?

Thanks.

GP doesn't do anything to WooCommerce queries.

And I think your PHP snippet is sound.

but to confirm, I've actually put up a replication attempt using your code and it works well on my GeneratePress site. You can check this link and you'll only see "Hoodies" and no "Uncategorized."

https://dev-generate-press.pantheonsite.io/shop/

Any chance you can provide us temporary backend access w/ all plugins disabled to check?

You can use the private information text field to provide the details. Thank you.

And here is the screenshot of how I have added it:

functions-php

Well, this is strange.

I deactivated all plugins, including WooCommerce. Then I've added them back, one by one and now the code snippet works.

I am using a staging environment on SiteGround, and they assure me they don't do caching on staging environments.

In case someone else stumbles on to this, two lessons here:

  1. the code snippet is working fine
  2. if it's not working for you make sure you clear the cache (if you have one) and two deactivate and reactivate the plugins.

Apologies to GP for wasting your time :)

Nice one. Glad you got it sorted. Thank you for letting us know. :)

This archived topic is closed to new replies.