Back WordPress

Create Dropdown List of Posts from a Category

WRITTEN BY ON 07 Feb 2011
13,350 VIEWS • SHARES
0 comments

You want to list posts from a category but don’t have much space for the list in your site? Simple, make the list in a dropdown box. Here, I’ll share the code to create this dropdown box. Also, as the result of this code, when the post in the list is selected, the user will be taken to that post.

PHP

Place the code below in your sidebar or wherever you want to put the list, in your site :
In this code, posts are from ‘uncategorized’ category. You can change the category to meet your needs.
If the list is to wide, you can set the width of select tag of the code via css.

<?php
	$cat_id = get_cat_ID('uncategorized');
	$args=array(
	  'cat' => $cat_id,
	  'post_type' => 'post',
	  'post_status' => 'publish',
	  'posts_per_page' => -1,
	  'caller_get_posts'=> 1
	);
	$my_query = null;
	$my_query = new WP_Query($args);
	if( $my_query->have_posts() ) {
?>
	<form name="jump">
		<select name="menu">
			<?php
			  while ($my_query->have_posts()) : $my_query->the_post(); ?>
				<option value="<?php the_permalink() ?>"><?php the_title(); ?></option>
				<?php

			  endwhile;
			}
			?>
		</select>
		<input type="button" onClick="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;" value="Go">
	</form>

<?php
	wp_reset_query();
?>

Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.