Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wordpress类别依赖的3个级别_Wordpress - Fatal编程技术网

wordpress类别依赖的3个级别

wordpress类别依赖的3个级别,wordpress,Wordpress,我的任务是在select中显示3个级别的类别国家-地区-城市 也就是说,如果我选择国家西班牙,西班牙的子类别应该显示在地区和城市中。当我选择马洛卡地区时,只有西班牙马洛卡地区的城市应该被列出 如果未选择国家,则应在选择中显示所有国家、地区和城市 我怎么做?我将非常感谢任何想法和提示。我找到了一个解决方案,它适合我 <?php $level = get_the_level( get_queried_object()->term_id, 'city' ); $parent_terms

我的任务是在select中显示3个级别的类别国家-地区-城市

也就是说,如果我选择国家西班牙,西班牙的子类别应该显示在地区和城市中。当我选择马洛卡地区时,只有西班牙马洛卡地区的城市应该被列出

如果未选择国家,则应在选择中显示所有国家、地区和城市


我怎么做?我将非常感谢任何想法和提示。

我找到了一个解决方案,它适合我

<?php
$level = get_the_level( get_queried_object()->term_id, 'city' );
$parent_terms = get_terms(array (
    'taxonomy' => 'city',
    'parent' => 0,
    'orderby' => 'slug',
    'hide_empty' => false,
    'fields' => 'ids'
)); ?>

<div class="blog-nav">
    <div class="blog-nav__title"><?php _e('Select country', 'bike'); ?></div>
    <select class="modal-form__input" onchange="document.location.href=this.options[this.selectedIndex].value;">
        <option value="<?php echo get_post_type_archive_link('rental-stations'); ?>"><?php _e('All', 'bike'); ?></option>   
        <?php foreach ($parent_terms as $pterm) {
            $check_term = get_terms(array (
                'taxonomy' => 'city',
                'child_of' => $pterm,
                'orderby' => 'slug',
                'hide_empty' => false,
                'fields' => 'ids'
            ));
            if ( get_queried_object()->term_id == $pterm || in_array(get_queried_object()->term_id, $check_term) ) {
                $selected = 'selected';
            } else {
                $selected = '';
            }
            $option = '<option '.$selected.' value="'.get_category_link($pterm).'">';
            $option .= get_term( $pterm )->name;
            $option .= '</option>';
            echo $option;
        } ?>
    </select>
</div>

<div class="blog-nav">
    <div class="blog-nav__title"><?php _e('Select region', 'bike') ?></div>
    <select class="modal-form__input" onchange="document.location.href=this.options[this.selectedIndex].value;">
        <option value="<?php echo get_category_link(get_term(get_queried_object()->term_id)->parent); ?>"><?php _e('All', 'bike'); ?></option>   
        <?php 
        if ($level == 0) {
            $child_of = get_queried_object()->term_id;
            $parent = get_queried_object()->term_id;
        } elseif ($level == 1) {
            $child_of = get_term(get_queried_object()->term_id)->parent;
            $parent = get_queried_object()->term_id;
        } elseif ($level == 2) {
            $child_of = get_term(get_term(get_queried_object()->term_id)->parent)->parent;
            $parent = get_term(get_queried_object()->term_id)->parent;
        }
        $terms = get_terms(array (
            'taxonomy' => 'city',
            'orderby' => 'slug', 
            'hide_empty' => false,
            'fields' => 'ids',
            'child_of' => $child_of
        ));
        foreach ($terms as $term) {
            if ( get_term($term)->parent && category_has_children($term, 'city') ) { 
                if ( get_queried_object()->term_id == $term || get_term(get_queried_object()->term_id)->parent == $term ) {
                    $selected = 'selected';
                } else {
                    $selected = '';
                }
                $option = '<option '.$selected.' value="'.get_category_link($term).'">';
                $option .= get_term( $term )->name;
                $option .= '</option>';
                echo $option;
            }
        } ?>
    </select>
</div>

<div class="blog-nav">
    <div class="blog-nav__title"><?php _e('Select city', 'bike') ?></div>  
    <select class="modal-form__input" onchange="document.location.href=this.options[this.selectedIndex].value;">
        <option value="<?php echo get_category_link(get_term(get_queried_object()->term_id)->parent) ?>"><?php _e('All', 'bike'); ?></option>   
        <?php $cterms = get_terms(array (
            'taxonomy' => 'city',
            'orderby' => 'slug', 
            'hide_empty' => false,
            'fields' => 'ids',
            'child_of' => $parent
        ));
        foreach ($cterms as $cterm) {
            if (get_term($cterm)->parent && !category_has_children($cterm, 'city')) { 
                if ( get_queried_object()->term_id == $cterm ) {
                    $selected = 'selected';
                } else {
                    $selected = '';
                }
                $option = '<option '.$selected.' value="'.get_category_link($cterm).'">';
                $option .= get_term( $cterm )->name;
                $option .= '</option>';
                echo $option;
            }
        } ?>
    </select>
</div>