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.

Post a Comment

0 Comments