Wordpress

How to create a dropdown list of posts in WordPress by category

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

 

 

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.