How to Create a Drop Down List for Posts on WordPress
The following two options will allow you to create a drop down list for your posts. I went looking for a widget to do this but couldn’t find one. So the solutions below are somewhat manual in nature, but they still work.
The first solution is fairly simple. Just insert the following code into your theme — for example, in your sidebar (Appearance > Editor > Sidebar).
You can control the number of posts it displays by changing the number in this line of the code:
$args = array( 'numberposts' => -1);
To show all your posts, leave the “-1” in place. To show ten posts, for example, replace the “-1” with the number “10.”
<form action="<? bloginfo('url'); ?>" method="get"> <select name="page_id" id="page_id"> <?php global $post; $args = array( 'numberposts' => -1); $posts = get_posts($args); foreach( $posts as $post ) : setup_postdata($post); ?> <option value="<? echo $post->ID; ?>"><?php the_title(); ?></option> <?php endforeach; ?> </select> <input type="submit" name="submit" value="view" /> </form>
And here’s the code in action.
(The original source for this code can be found at the following link; however, the last time I visited it, my anti-virus software gave me a warning message, so beware if you want to visit it: http://wpsnipp.com/index.php/posts/display-all-posts-within-a-dropdown-menu/)
The next option is what I’ll call a “semi-manual” plugin from Dragon Design.
I call this semi-manual because if you go to the link above, you are provided with the code for the plugin, but you will need to make your own file for it. That’s easy enough to do. Just open up a text editor like Notepad, copy and paste the code into it, and then save it as a file with a .php extension. For some reason I couldn’t get it to upload through the Admin area, but if you go directly to your server, you can upload it there.
Something else to note before we get into the details is that this plugin hasn’t been updated in a few years. That said, it seemed to work fine. Past that, however, some don’t like using older plugins even if they work for fear of potential security issues.
Once installed, you will need to place code in your theme’s files where you want the drop down list to appear. (You can draw posts from all categories or a specific category.) There are also shortcodes for the plugin. All of this is explained on the plugin’s homepage.
So why do all this work? Well, once installed and activated, the plugin gives you some options that may be important to you. Here’s a look at the settings page.
Here’s the plugin in action.
If anyone knows of an up-to-date (and preferably widgetized) plugin that will do this, then please let us know in the comments.
Photo: Web Navigation Menu Element from BigStock