I have the following code on my website:

<div class="blog-cats drop">

<label class="custom-select">
<select class="styledSelect1" name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'>


Instead of display posts by archive I want to display them by categories. BUT I am trying to get it so that it only show sub categories of a particular category called 'Blog'

Does anybody have any example of how I can go about achieving this?

Thanks for your help, it's appreciated :-)


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():

    <option value=""><?php echo esc_attr(__('Select Event')); ?></option> 
    $categories = get_categories('child_of=10'); 
    foreach ($categories as $category) {
        $option = '<option value="/category/blog/'.$category->category_nicename.'">';
        $option .= $category->cat_name;
        $option .= '</option>';
        echo $option;

Most of the code is similar to the one you're using. The only things worth explaining are:

  1. 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.

  2. 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.

Your Answer

