Last active
July 24, 2019 17:35
-
-
Save kartikparmar/1b43335ba7b481e78fffa536742a44f2 to your computer and use it in GitHub Desktop.
hide_wc_category_from_shop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Show products only of selected category. | |
*/ | |
function get_subcategory_terms( $terms, $taxonomies, $args ) { | |
$new_terms = array(); | |
$hide_category = array( 126 ); // Ids of the category you don't want to display on the shop page | |
// if a product category and on the shop page | |
if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) { | |
foreach ( $terms as $key => $term ) { | |
if ( ! in_array( $term->term_id, $hide_category ) ) { | |
$new_terms[] = $term; | |
} | |
} | |
$terms = $new_terms; | |
} | |
return $terms; | |
} | |
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi the above is correct only for the shop page. If you are in the product page the categories will not be hidden. You will also loose the original order of the categories defined in the category setting page.
Your code could be improved like following:
Also if you want to remove the category page of the hidden category you have to add the following (otherwise url http://yourdomain.com/category/category_slug will be still visible):