I am attempting to display a category dropdown (select) with parent categories and child categories tagged under the child category. Example:
parent A
parent B
parent C
- child of C
- child of C
- child of C
parent D
parent E
- child of E
- child of E
parent F
Instead what I am getting is the following:
parent A
parent B
parent C
parent D
parent E
parent F
- child of C
- child of C
- child of C
- child of E
- child of E
Here is my shortcode:
function categories_dropdown() {
$args = array(
'taxonomy' => 'my_custom_taxonomy',
'hide_empty' => false,
'echo' => true,
'hierarchical' => true,
'show_count' => false,
'orderby' => 'parent',
);
$categories = get_categories($args);
$select = "<select name='cat' id='categories_dropdown' class='postform'>n";
$select.= "<option value='-1'>Select category</option>n";
foreach($categories as $category){
if($category->count > 0){
if($category->parent!=0) {
$select.= "<option value='".$category->slug."'> - ".$category->name." (". $category->count .")</option>";
} else {
$select.= "<option value='".$category->slug."'>".$category->name." (". $category->count .")</option>";
}
}
}
$select.= "</select>";
return $select;
}
add_shortcode('categories-dropdown', 'categories_dropdown');
My question here is, which of the arguments are responsible for the correct ordering of the children under the parent taxonomy category? Is this at all done with arguments or is this a matter of the way I have to code the display of the options dropdown? I am really thankful for your time.