How to hide subcategories in WP?

Rate this post

this loop shows all tha categories of woocommerce products but i want to hide subcategories. I only want to filter parent categories and hide subcategories from the loop.

                        $orderby = 'name';
                        $order = 'asc';
                        $hide_empty = false ;
                        $product_categories = get_terms( 'product_cat', $cat_args );
                        $wp_query->set('tax_query', array(
                            array (
                                'taxonomy' => 'product_cat',
                                'field' => 'slug',
                                'terms' => $wp_query->query_vars['product_cat'],
                                'include_children' => false

                        if( !empty($wp_query) ){
                            foreach ($product_categories as $key => $category) {
                                $thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
                                $image = wp_get_attachment_url( $thumbnail_id );

                                    echo'            <div class="swiper-slide">';
                                    echo'            <div class="menu-item menu-item-2">';
                                    echo'                <div class="image">';
                                    echo'                    <img src="'. $image . '" alt="" class="resp-img">';
                                    echo'                    <div class="vertical-align full menu-button">';
                                    echo'                        <a href="'.get_term_link($category).'" class="page-button button-style-1"><span class="txt">ACQUISTA</span></a>';
                                    echo'                    </div>';
                                    echo'                </div>';
                                    echo'                <div class="text">';
                                    echo'                    <div class="empty-sm-20 empty-xs-20"></div>';
                                    echo'                    <h4 class="h5 caption tt"><a href="'.get_term_link($category).'" class="link-hover-line">'. $category->name . '</a></h4>';
                                    echo'                    <div class="empty-sm-5 empty-xs-5"></div>';
                                    echo'                    <div class="simple-text">';
                                    echo'                        <p>'. $category->description . '</p>';
                                    echo'                    </div>';
                                    echo'                </div>';
                                    echo'            </div>';
                                    echo'        </div>';


                        }  wp_reset_query()?>



get_terms() should accept arguments.

get_terms( array|string $args = array(), array|string $deprecated = ” )

Retrieves the terms in a given taxonomy or list of taxonomies.

adding 'parent' => 0 to your $product_categories arguments $cat_args should do the trick. If 'parent' => 0 is passed, only top-level terms will be returned.

$cat_args = [
    'parent' => 0,

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button