WordPress Query to Show Post Title as Dropdown List

In WordPress you can show no. of most recent posts or popular posts in sidebar. You can also show post title as drop down list. You can add this feature in single post page or in sidebar or footer widget area. Here we are going to share the simple WordPress query to show post title as dropdown list. If you have good knowledge in CSS then you can make it more attractive. We have added the query in right sidebar in our example.

You have to allow php in widget area at first and then add the following code in widget area:

[html]

<form action="<? bloginfo(‘url’); ?>" method="get">
<select name="page_id" id="page_id">
<?php
global $post;
$args = array( ‘numberposts’ =>10);
$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 The Post" />
</form>

[/html]

show-post-as-dropdown

In our example we have shown 10 posts if you want show more than 10 posts then change the number in “numberposts’ =>10“. For say you want to show 20 posts then change the number to 20. Here is the front-end display in sidebar area:

show-post-as-dropdown1

show-post-as-dropdown2

 

Author: mytutorialguru

Share This Post On

Submit a Comment

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