0

Hello i create a function to display list of subcategory on master category page. With my functions now i see also category that no have product (empty). I need hide this empty category..

My master category and list subcategory code

              <h1 style="font-size: 36px; font-weight: 400; letter-spacing: 2px;   font-family: 'Oswald';"><?php echo $cat->name; ?></h1>
            <p><?php echo $cat->description ; ?></p>
                <!--<?php if(is_array($parent_categories_ids)){ echo'<pre>',print_r($parent_categories_ids),'</pre>'; }?> -->
            <div class="list">
            <ul>
            <?php foreach($term_children as $child) :
                    $term = get_term_by( 'id', $child, 'product_cat' );
            ?>
<?php                 if( $child->count <= 0 ) {
        continue;
    } ?>
                <li><a href="<?php echo get_term_link( $child, 'product_cat' )?>"><?php echo $term->name; ?></a></li>
            <?php endforeach; ?>
            </ul>
        </div>

Code to try hide

 <?php // Skip empty terms
        if( $child->count <= 0 ) {
            continue;
        } ?>

But not work...

0

By your code i'm assuming your $child is the id of an term, the term object is being stored in the $term variable, so you can do this:

<?php if( $term->count > 0 ): ?>
  <li><a href="<?php echo get_term_link( $child, 'product_cat' )?>"><?php echo $term->name; ?></a></li>
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.