0

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;'>
<option value=""><?php echo attribute_escape(__('Select Month')); ?></option>
<?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?>     </select>
</label>

</div>

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

1 Answer 1

1

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:

  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

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.