按选择顺序列出城市A自定义分类

时间:2020-03-13 作者:Emma Expat

下面的代码按升序列出城市,我想根据我从前端选择城市的顺序列出城市。它是由主题创建的短代码。城市是这里的一种自定义分类法。

我应该怎么做请建议?

按ASC顺序列出的城市截图:https://prnt.sc/s87to8 后端城市选择https://prnt.sc/s87gyi

view.php

<?php
if (!defined(\'FW\')){
    die(\'Forbidden\');
}
/**
 * @var $atts
 */

$title              = !empty( $atts[\'title\'] ) ? $atts[\'title\'] : \'\';
$description        = !empty( $atts[\'description\'] ) ? $atts[\'description\'] : \'\';
$cities             = !empty( $atts[\'city\'] ) ? $atts[\'city\'] : array();
$city_buttons       = !empty( $atts[\'city_buttons\'] ) ? $atts[\'city_buttons\'] : array();

?>
<?php if ( !empty( $cities ) ) { ?>
        <div class="tg-popularcities">                  
        <?php 
            $terms = get_terms( array(
                \'taxonomy\'      => \'cities\',
                \'include\'       => $cities,
                \'hide_empty\'    => 0
            ) );

            if ( !empty( $terms ) ){                            
                foreach ( $terms as $key => $value ) {
                    $country_name = \'\';                  
                    $get_cities_meta = fw_get_db_term_option( $value->term_id, \'countries\' );
                    if (!empty($get_cities_meta[\'country\'][0])) {
                        $country_id = $get_cities_meta[\'country\'][0];
                        $country_term = get_term($country_id, \'countries\');
                        $country_name = $country_term->slug;                                                  
                    }

                    $city_name = $value->slug;  
                    $custom_url = "?country=".$country_name."&city=".$city_name;                            
                    $term_data = fw_get_db_term_option( $value->term_id, \'cities\' );                        
                    $cat_image = !empty( $term_data[\'image\'][\'url\'] ) ? $term_data[\'image\'][\'url\'] : get_template_directory_uri().\'/images/locations/city.jpg\';
                    $total_users    = listingo_get_total_users_under_taxanomy($city_name,\'number\',\'city\');
                ?>                                      
                <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
                    <div class="tg-topcity">
                        <figure class="tg-cityimg">                                 
                            <img src="<?php echo esc_url( $cat_image ); ?>" alt="<?php esc_html_e(\'City Image\', \'listingo\'); ?>">                                   
                            <figcaption>                                        
                                <h3><a href="<?php echo esc_url( get_term_link( $value->term_id, \'cities\' ) ); echo esc_attr( $custom_url  ); ?>"><?php echo esc_attr( $value->name ); ?></a></h3>
                                <span><?php echo esc_attr( $total_users ); ?>&nbsp;<?php esc_html_e(\'Listings\', \'listingo\'); ?></span>                                      
                            </figcaption>
                        </figure>
                    </div>
                </div>
            <?php } } ?>
        </div>
    <?php } ?>

options.php

<?php

if (!defined(\'FW\'))
    die(\'Forbidden\');

$options = array(
    \'title\' => array(
        \'label\' => esc_html__(\'Heading\', \'listingo\'),
        \'desc\' => esc_html__(\'Add section heading. leave it empty to hide.\', \'listingo\'),
        \'type\' => \'text\',
    ),
    \'description\' => array(
        \'type\' => \'wp-editor\',
        \'label\' => esc_html__(\'Description\', \'listingo\'),
        \'desc\' => esc_html__(\'Add section description. leave it empty to hide.\', \'listingo\'),
        \'tinymce\' => true,
        \'media_buttons\' => false,
        \'teeny\' => true,
        \'wpautop\' => false,
        \'editor_css\' => \'\',
        \'reinit\' => true,
        \'size\' => \'small\', // small | large
        \'editor_type\' => \'tinymce\',
        \'editor_height\' => 200
    ),
    \'city\' => array(
        \'type\' => \'multi-select\',
        \'label\' => esc_html__(\'Select Cities\', \'listingo\'),
        \'population\' => \'taxonomy\',
        \'source\' => \'cities\',
        \'prepopulate\' => 500,
        \'desc\' => esc_html__(\'Show cities as per your selection. Leave it empty to show from all.\', \'listingo\'),
    ),
    \'city_buttons\' => array(
        \'type\' => \'addable-box\',
        \'label\' => esc_html__(\'Add Button\', \'listingo\'),
        \'desc\' => esc_html__(\'\', \'listingo\'),
        \'box-options\' => array(
            \'button_text\' => array(\'type\' => \'text\'),
            \'button_link\' => array(\'type\' => \'text\'),
        ),
        \'template\' => \'{{- button_text }}\', // box title
        \'box-controls\' => array(// buttons next to (x) remove box button
            \'control-id\' => \'<small class = "dashicons dashicons-smiley"></small>\',
        ),
        \'limit\' => 1, // limit the number of boxes that can be added
        \'add-button-text\' => esc_html__(\'Add\', \'listingo\'),
        \'sortable\' => true,
    ),
);

1 个回复
最合适的回答,由SO网友:Emma Expat 整理而成

通过添加“orderby”=>“term\\u order”,line解决了此问题。

$terms = get_terms( array(
                    \'taxonomy\'      => \'cities\',
                    \'include\'       => $cities,
                    \'orderby\'  => \'term_order\',
                    \'order\'    => \'ASC\',
                    \'hide_empty\'    => 0
                ) );

相关推荐

如何按GET_TERMS显示所有类别

我正在使用下面的代码,但只显示父类别,而我有许多子类别。我想显示所有类别,包括家长和孩子<label for=\"<?php echo $this->get_field_id( \'link\' ); ?>\"><?php _e( \'دسته بندی\' ); ?></label> <select id=\"<?php echo $this->get_field_id(\'link\'