taxonomy - How to show children terms even if they are empty

admin2025-06-03  3

First of all im not good at PHP so it might be the problem... So after trying to do what I want without success I want to ask our help. For most of you probably it will be easy as eating breakfast.

I've got this piece of code that shows child terms only if they are not empty and i want to change it to display childs even if they are empty. It's a part of Adifier WP Theme.

            <?php
            if( !empty( $term->children ) ){
                ?>
                <ul class="list-unstyled hidden">
                    <?php self::taxonomy_listing( $name, $term->children, $taxonomy, $selected_term ); ?>
                </ul>
                <?php
            }
            ?>

This is a full function code. Maybe it will help also:

    /*
* Display filter taxonomies
*/
static public function taxonomy_listing( $name, $terms, $taxonomy, $selected_term ){
    $search_more_less = adifier_get_option( 'search_more_less' );
    $counter = 0;
    foreach( $terms as $term ){
        $counter++;
        ?>
        <li class="<?php echo !empty( $search_more_less ) && $counter > $search_more_less ? esc_attr( 'term-hidden' ) : '' ?>">
            <div class="styled-radio">
                <input type="radio" name="<?php echo esc_attr( $name ) ?>" value="<?php echo esc_attr( $term->term_id ) ?>" id="<?php echo esc_attr( $name ) ?>-<?php echo esc_attr( $term->term_id ) ?>" <?php echo  $term->term_id == $selected_term ? esc_attr( 'checked="checked"' ) : '' ?>>
                <label for="<?php echo esc_attr( $name ) ?>-<?php echo esc_attr( $term->term_id ) ?>"><?php echo  $term->name ?></label>
                <?php
                    if( !empty( $term->children ) ){
                        ?>
                        <a href="javascript:void(0);"><i class="aficon-angle-down"></i></a>
                        <?php
                    }
                ?>                  
            </div>
            <?php
            if( !empty( $term->children ) ){
                ?>
                <ul class="list-unstyled hidden">
                    <?php self::taxonomy_listing( $name, $term->children, $taxonomy, $selected_term ); ?>
                </ul>
                <?php
            }
            ?>
        </li>
        <?php
    }

    if( !empty( $search_more_less ) && $counter > $search_more_less ){
        ?>
        <li class="toggle-more-less-wrap">
            <a href="javascript:void(0)" data-less="<?php esc_attr_e( 'Show Less', 'adifier' ) ?>" data-more="<?php esc_attr_e( 'Show More', 'adifier' ) ?>" class="toggle-more-less"><span><?php esc_html_e( 'Show More', 'adifier' ) ?></span> <i class="aficon-caret-down"></i></a>
        </li>
        <?php
    }
} 

I will be very grateful for your help!

First of all im not good at PHP so it might be the problem... So after trying to do what I want without success I want to ask our help. For most of you probably it will be easy as eating breakfast.

I've got this piece of code that shows child terms only if they are not empty and i want to change it to display childs even if they are empty. It's a part of Adifier WP Theme.

            <?php
            if( !empty( $term->children ) ){
                ?>
                <ul class="list-unstyled hidden">
                    <?php self::taxonomy_listing( $name, $term->children, $taxonomy, $selected_term ); ?>
                </ul>
                <?php
            }
            ?>

This is a full function code. Maybe it will help also:

    /*
* Display filter taxonomies
*/
static public function taxonomy_listing( $name, $terms, $taxonomy, $selected_term ){
    $search_more_less = adifier_get_option( 'search_more_less' );
    $counter = 0;
    foreach( $terms as $term ){
        $counter++;
        ?>
        <li class="<?php echo !empty( $search_more_less ) && $counter > $search_more_less ? esc_attr( 'term-hidden' ) : '' ?>">
            <div class="styled-radio">
                <input type="radio" name="<?php echo esc_attr( $name ) ?>" value="<?php echo esc_attr( $term->term_id ) ?>" id="<?php echo esc_attr( $name ) ?>-<?php echo esc_attr( $term->term_id ) ?>" <?php echo  $term->term_id == $selected_term ? esc_attr( 'checked="checked"' ) : '' ?>>
                <label for="<?php echo esc_attr( $name ) ?>-<?php echo esc_attr( $term->term_id ) ?>"><?php echo  $term->name ?></label>
                <?php
                    if( !empty( $term->children ) ){
                        ?>
                        <a href="javascript:void(0);"><i class="aficon-angle-down"></i></a>
                        <?php
                    }
                ?>                  
            </div>
            <?php
            if( !empty( $term->children ) ){
                ?>
                <ul class="list-unstyled hidden">
                    <?php self::taxonomy_listing( $name, $term->children, $taxonomy, $selected_term ); ?>
                </ul>
                <?php
            }
            ?>
        </li>
        <?php
    }

    if( !empty( $search_more_less ) && $counter > $search_more_less ){
        ?>
        <li class="toggle-more-less-wrap">
            <a href="javascript:void(0)" data-less="<?php esc_attr_e( 'Show Less', 'adifier' ) ?>" data-more="<?php esc_attr_e( 'Show More', 'adifier' ) ?>" class="toggle-more-less"><span><?php esc_html_e( 'Show More', 'adifier' ) ?></span> <i class="aficon-caret-down"></i></a>
        </li>
        <?php
    }
} 

I will be very grateful for your help!

Share Improve this question asked Feb 9, 2019 at 11:59 Christian MateChristian Mate 11 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 0

Ok, I think I got it! :D After some research in theme functions how taxonomy hierarchy structure is done I modified the code as below (don't know if it's coded good but it works):

/*
* Display filter taxonomies
*/
static public function taxonomy_listing( $name, $terms, $taxonomy, $selected_term, $hide_empty = false ){
    $search_more_less = adifier_get_option( 'search_more_less' );
    $counter = 0;

    foreach( $terms as $term ){
        $counter++;
        ?>
        <li class="<?php echo !empty( $search_more_less ) && $counter > $search_more_less ? esc_attr( 'term-hidden' ) : '' ?>">
            <div class="styled-radio">
                <input type="radio" name="<?php echo esc_attr( $name ) ?>" value="<?php echo esc_attr( $term->term_id ) ?>" id="<?php echo esc_attr( $name ) ?>-<?php echo esc_attr( $term->term_id ) ?>" <?php echo  $term->term_id == $selected_term ? esc_attr( 'checked="checked"' ) : '' ?>>
                <label for="<?php echo esc_attr( $name ) ?>-<?php echo esc_attr( $term->term_id ) ?>"><?php echo  $term->name ?></label>
                <?php
                    if( !empty( $term->children ) ){
                        ?>
                        <a href="javascript:void(0);"><i class="aficon-angle-down"></i></a>
                        <?php
                    }
                ?>                  
            </div>
            <?php
            if( !empty( $term->children = adifier_get_taxonomy_hierarchy( $taxonomy, $term->term_id, $hide_empty ) ) ){
                ?>
                <ul class="list-unstyled hidden">
                    <?php self::taxonomy_listing( $name, $term->children, $taxonomy, $selected_term ); ?>
                </ul>
                <?php
            }
            ?>
        </li>
        <?php
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748924113a314861.html

最新回复(0)