$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>taxonomy - How to show children terms even if they are empty|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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
    }
Post a comment

comment list (0)

  1. No comments so far