How to Display All Posts in WordPress Sidebar

by on April 20, 2010

in CMS, Tips

Working for one of my client he had a very unique requirement. “He wanted to show all the posts in his blog as a combo box where user can select any post and visit that post”.

Could not find anything and so I had to do the coding myself but when I used the default WordPress looping functions it had some issues on the category pages and the loop to show all posts interfered with the main page loop and so I had to do a new looping.

The below code will not interfere with any of the existing loops in the page and can even be used on 404 error pages.

<script language="JavaScript">
<!--
 function gotourl() {
   // JavaScript function to change the user to the url of the author
   theSel = document.getElementById("AllPosts").value;
   if ("" != theSel) location.href = theSel;
 }
//-->
</script>
<?php
$featuredPosts = new WP_Query();
$featuredPosts->query('showposts=-1');
?>
<select id="AllPosts" name="AllPosts" onChange="gotourl();"><option>Select Your Celebrity</option>
  <?php
    while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
      <option value="<?php the_permalink() ?>"><?php the_title(); ?></option>
  <?php endwhile; ?>
</select>

Enjoy!!!

Sign up for Free Course on Ways to Make Money Online?

Don't wanna sign up but prefer posts delivered to your Inbox?

But if you are not planning to subscribe through Email. Try subscribing to RSS.

Further Reading:

  1. Best WordPress SEO Tips
  2. SEO for WordPress Categories
  3. vBulletin Blog Does not Display Ads From Ad Templates
  4. Blogspot Vs WordPress
  5. Best WordPress SEO Permalinks

{ 2 comments… read them below or add one }

CMS Application March 24, 2011 at 9:38 am

Awesome post! It really helped me a lot! Thanks for sharing it.

Reply

Shabbir March 24, 2011 at 11:09 am

The pleasure is all mine.

Reply

Leave a Comment

Spam protection by WP Captcha-Free

Previous post:

Next post: