Sometimes there is the need to have somewhere in your wordpress site a drop down menu with posts from your blog. If you want to do this for posts from a specific category there is a pretty easy way. The code for it is
<form action="<? bloginfo('url'); ?>" method="get">
<select name="page_id" id="page_id">
<?php
global $post;
$args = array( 'numberposts' => -1, 'category' => 2 );
$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="READ" />
</form>
You can add a class inside the form tag to add some css styling for your dropdown (size, background color, fonts etc…). Inside the <input type=”submit” /> tag you can change the value to something else, depending on what you want it to say. The most essential part is the $args variable. The number after the ‘category’ represents the id number of your posts category, which can be found if you hover over the specific posts category in your dashboard, at the bottom left part of your browser, something like ….category&tag_ID=2…. . In my case it was 2. In your case it will be 2 or something else.
If you don’t need to seperate the category, but want to display posts regardless of category, then delete the
, 'category' => 2
from your variable. Don’t forget the comma..
Also, if you live the ‘numberposts’ -1, you will display all the posts in your drop down. If you define a specific number (5, 7, 13..) you will have such number of posts in your drop down.
And that’s it. You place this code inside the template file you want the drop down to appear (sidebar, footer etc..)