How to create a dropdown list of posts in WordPress by category
You may find yourself in need of creating a drop down box listing of all your posts on your blog or maybe a subset of posts based on category. ย There are a couple of ways to do this both by builtin functions in WordPress as well as by using a plugin to achieve the same. ย Also, a blend of the two exists.
WordPress Function
WordPress has a builtin function calledย wp_get_archives which will display a date based listing of posts that can be used in your wordpress template. ย Use the code below to create a dropdown listing of your posts displayed alphabetically:
<select name=\"archive-dropdown\" onChange='document.location.href=this.options[this.selectedIndex].value;'> <option value=\"\"><?php echo attribute_escape(__('Select Post')); ?></option> <?php wp_get_archives('type=alpha&format=option&show_post_count=0'); ?> </select>
Notice in the code above we have added the option “alpha” which tells the drop down to sort alphabetically instead of by post date. ย Simply leave out this option if you want the drop down to display posts by post date. ย also, you can alter the text “Select Post” to whatever you want to be displayed in the drop down menu before the user clicks.
If you want to insert the menu created above directly into your Page/Post, simply use a plugin such asย Exec PHPย to allow using PHP code directly in your Page/Post.
Cons: ย The major con of using theย wp_get_archives function is that it doesn’t have a builtin way to display only a subset of posts based on category, rather it displays from all categories. ย The following are valid values to pass to the function:
- yearly
- monthlyย – Default
- daily
- weekly
- postbypostย (posts ordered by post date)
- alphaย (same asย postbypostย but posts are ordered by post title)
Augment wp_get_archives
What we love about WordPress is the tremendous development with the platform. ย Usually, if there is something you can’t do with a builin function, you can look for a plugin that someone has written to accomplish the task. ย Checkout theย Wordpress pluginย Archives for a categoryย which augments the builtin function to accept a category parameter. ย This is a great way to built a dropdown menu using the builtin function along with the ability to narrow the dropdown scope to a specific category or categories.
Plugin only approach
If you want to simply use a plugin only to accomplish the functionality above, checkout theย Dropdown post list pluginย for WordPress by Dagon Design. ย The plugin creates a nice GUI interface in the admin menu where you can configure your drop down list. ย After you configure your options you can simply use the shortcode created by the plugin to insert into your Page/Post.