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.

 

Insert Code

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.

Featured Plugin - WordPress Ecommerce Shopping Cart Plugin

Out of all the WordPress ecommerce plugins available, MarketPress has got to be the winner - easy to configure, powerful functionality, multiple gateways and more. A simply brilliant plugin!
Find out more

(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/)

 

Semi-Manual Plugin

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.

 

Featured Plugin - WordPress Facebook Plugin

Would you like to add Facebook comments, registration, 'Like' buttons and autoposting to your WP site? Well, The Ultimate Facebook plugin has got that all covered!
Find out more

Photo: Web Navigation Menu Element from BigStock

 

Tags

Comments (5)

  1. Hi,
    This is neat stuff.
    I have 2 questions:
    1) Why not use the “wp_dropdown_pages” function?
    2) How can I display this in the backend (e.g. in a metabox for an article?)
    Thanks :)

    • Ok,
      Sorry about this…
      I can’t tell the answer to question no. 1, but to question no. 2 I’ve created an implementation:

      *** Start of code ***

      ‘product’ );
      ?>

      [ — Select Product Page — ]
      ID ) ? ‘ selected=”selected” ‘ : ”;
      echo ‘ID . ‘”‘ . $selected . ‘>’ . $page->post_title . ”;
      endforeach; endif;
      ?>

      *** End of code ***

      This is only the select box part, of course, and it’s only a part of adding a metabox to a post type (for instance, if you need to connect the “product” post type to a “vendor” post type – like I needed).

  2. Joe Foley – your code no longer / does not work.

    It also prevents any post from loading freezing a wp-site on one post.

    Declaring $post as a global variable in one random query will always cause trouble and further more setup_postdata offers no way to reset / unset postdata for the main post on the page.

    This code works:

    *** Start of code ***

    <form action="” method=”get”>

    1,
    ‘posts_per_page’ => -1,
    ‘orderby’ => title )
    query_posts($args); if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    <option value="ID; ?>”>

    *** End of code ***

Participate