You can use a similar block of code to display the categories in a dropdown. I have picked the following example from WordPress codex article on get_categories():
<select name="event-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr(__('Select Event')); ?></option>
<?php
$categories = get_categories('child_of=10');
foreach ($categories as $category) {
$option = '<option value="/category/blog/'.$category->category_nicename.'">';
$option .= $category->cat_name;
$option .= ' ('.$category->category_count.')';
$option .= '</option>';
echo $option;
}
?>
Most of the code is similar to the one you’re using. The only things worth explaining are:
-
The
child_of=10
assumes your parent category has an id 10. You’ll need to replace it with the real id of your category. -
The
option value="/category/blog/
assumes that your permalinks are like this. If you have a different structure, you’ll need to change it accordingly.
You can check out the article on get_categories() on WordPress Codex for more examples and options. For further parameters to pass to get_categories($args)
array refer to this page of the WordPress Developer documentation.