Monday, June 28, 2010

Display a List of Upcoming Posts in WordPress

When you have completed a post in WordPress, you have the ability to save it as a draft, publish it immediately or schedule a future date when it will be published automatically.

This is extremely helpful if you want to plan ahead or if you use your WP site for events. Creating a simple list of upcoming posts lets your visitors know what they can look forward to and is pretty simple to do.




The following code can be placed in your sidebar on within any
template.
<?php
$futurePosts = new WP_Query();
$futurePosts->query('showposts=5&post_status=future&order=ASC');

if ($futurePosts->have_posts()) : while ($futurePosts->have_posts()) :
?>
<ul>
<li><?php the_title(); ?><br /><small><?php the_time(get_option('date_format'); ?></small></li>
</ul>
<?php endwhile; else: ?>
<ul>
<li>No upcoming events.</li>
</ul>
<?php endif; ?>

If you want to only display future posts from a specific category, you

$futurePosts->query('showposts=5&post_status=future&order=ASC&cat=12');

can add a cat=$id to the query like so:

No comments:

Post a Comment