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

Highlights

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

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 has over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, Brandon 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.

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.