admin管理员组

文章数量:1125559

I'm wondering how to sort a custom post's categories in the selection field which I've circled in the screenshot?

And I have the following code which generates category listing

 $args=array(
                'class'       => 'select-submit2',
                'hide_empty'  => false,
                'selected'    => $prop_category_selected,
                'name'        => 'prop_category',
                'id'          => 'prop_category_submit',
                'orderby'     => 'NAME',
                'order'       => 'ASC',
                'show_option_none'   => __('None','wpestate'),
                'taxonomy'    => 'property_category',
                'hierarchical'=> true
            );
        wp_dropdown_categories( $args ); ?>

I'm wondering how to sort a custom post's categories in the selection field which I've circled in the screenshot?

And I have the following code which generates category listing

 $args=array(
                'class'       => 'select-submit2',
                'hide_empty'  => false,
                'selected'    => $prop_category_selected,
                'name'        => 'prop_category',
                'id'          => 'prop_category_submit',
                'orderby'     => 'NAME',
                'order'       => 'ASC',
                'show_option_none'   => __('None','wpestate'),
                'taxonomy'    => 'property_category',
                'hierarchical'=> true
            );
        wp_dropdown_categories( $args ); ?>
Share Improve this question edited Nov 25, 2020 at 22:50 Tom J Nowell 60.7k7 gold badges77 silver badges147 bronze badges asked Feb 11, 2017 at 20:38 anandmongolanandmongol 1371 gold badge2 silver badges13 bronze badges 6
  • Do you have access to the dropdown code? Its a widget? – David Lee Commented Feb 11, 2017 at 21:23
  • @DavidLee, I do have access to all the codes, but I don't know specifically which code is the dropdown code. I don't know whether, it's widget or not. But I used the advance search shortcode of theme developer using visual composer. I hope it helps. Thanx – anandmongol Commented Feb 11, 2017 at 21:35
  • Need more info to answer – user6686780 Commented Feb 11, 2017 at 22:18
  • can you inspect the HTML code, you can see if its a widget of the name there. – David Lee Commented Feb 11, 2017 at 23:14
  • Actually you can sort the categories of any post type in the categories screen under the post menu. Just drag and drop the categories in the order you want them to be. Usually all themes reflect this sorting of categories in the front end. – user6552940 Commented Feb 12, 2017 at 9:18
 |  Show 1 more comment

2 Answers 2

Reset to default 1

In your wp_dropdown_categories function call, you are using 'orderby' => 'NAME'. This argument is case-sensitive, and it should be 'orderby' => 'name' (all lowercase).

$args = array(
    'class'             => 'select-submit2',
    'hide_empty'        => false,
    'selected'          => $prop_category_selected,
    'name'              => 'prop_category',
    'id'                => 'prop_category_submit',
    'orderby'           => 'name', // Corrected to lowercase 'name'
    'order'             => 'ASC',
    'show_option_none'  => __('None', 'wpestate'),
    'taxonomy'          => 'property_category',
    'hierarchical'      => true
);

wp_dropdown_categories($args);

You need to change the orderby to ID:

            $args=array(
                'class'       => 'select-submit2',
                'hide_empty'  => false,
                'selected'    => $prop_category_selected,
                'name'        => 'prop_category',
                'id'          => 'prop_category_submit',
                'orderby'     => 'ID', //this changed to ID
                'order'       => 'ASC',
                'show_option_none'   => __('None','wpestate'),
                'taxonomy'    => 'property_category',
                'hierarchical'=> true
            );
        wp_dropdown_categories( $args ); ?>

本文标签: