Create Dropdown List of Posts from a Category

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();
?>

Share the love

Leave a Comment

Please keep in mind that comments are moderated and rel="nofollow" is in use. You can use [code][/code] if you want to write codes. Don't spam us :) Thanks!

Advertisement