Performancing Metrics

Display Upcoming Entries In Your Post With A WordPress Shortcode

Repost This

Since some days several contacts have been considering the option to display upcoming posts on their site. WordPress offers a really simple solution for this in the form of the attribute post_status. The post_status is stored in the wp_post table and has generally the attributes: draft, private, publish or static for pages. There is one more attribute which we will use in this example: future.

It is now simple to create a list of the 5 upcoming entries and display this in your theme, fe. in your sidebar.

<?php
$my_query = new WP_Query('post_status=future&showposts=5');
?>
<div class="sidebar-box">
    <?php
    if ($my_query->have_posts()) : while ($my_query->have_posts()) :
        $my_query->the_post();
        ?>
        <ul>
          <li>
           <?php the_title(); ?>
          </li>
        </ul>
    <?php endwhile; else: ?>
        <div>
        <ul>
          <li><?php _e('No upcoming Posts'); ?></li>
        </ul>
        </div>
    <?php endif; ?>
</div>

You can now easily style this in your CSS.

Use A Shortcode To Display Upcoming Entries In A Post

First, what are shortcodes? I wrote a small intro to shortcodes on Devlounge yesterday with some examples of how to build a shortcode.

Why would you want to display a list of upcoming posts within an entry? You could be writing a series and want to use your upcoming entries as an additional teaser in the hope that the reader will subscribe or return to your site. You could use a post template for this but it is very simple to create a shortcode. Once you have created this shortcode it is then very simple to add the list of upcoming posts anywhere in an entry.

Building the shortcode

For this example we are going to build a shortcode [upcoming] and will use the tag series as selector. Doing this, it will avoid that scheduled entries not tagged Series will not be displayed. This can be handy on multi-authored blogs with a regular, scheduled posting rhythm.

Add the following code to your functions.php.

function upcom($atts, $content = null) {
       extract(shortcode_atts(array(
               "num" => '5'
               "tag" => 'series'
       ), $atts));
       global $post;
       $myposts = get_posts('numberposts='.$num.'&post_status=future&order=DESC&orderby=date&tag='.$tag);
       $retour='<ul class="upcoming">';
       foreach($myposts as $post) :
               setup_postdata($post);
            $retour.='<li><a href="'.get_permalink().'">'.the_title("","",false).'</a></li>';
       endforeach;
       $retour.='</ul> ';
       return $retour;
}
add_shortcode('upcoming', 'upcom');

All you have to do now to display this list in an entry is use the shortcode [upcoming] in the editor.

In this example I used <ul class="upcoming"> to easily style the output in your CSS.

Modifying The Shortcode Function

The shortcode function above can easily be modified.

- The example returns 5 posts, change the value in line 3 to display more or less entries
- Change the tag you want to use in the same way as you would change the number of displayed entries (line 4).

Repost This

Categories: WordPress Tips, WordPress Tutorials
Tags: , , ,

This post was written by . You can visit the for a short bio, more posts, and other information about the author.


Comment with Your Facebook Account

Comments

  1. Kian Ann says: 1/2/2010

    Whoa thanks! Didnt know WordPress had a add_shortcode() function to add it so easily!

    Is there one to add that shortcode as a button in the post edit screen?

    Reply

  2. Adriana says: 2/24/2012

    thanks for share!

    Reply

Content


Receive the top stories from BloggingPro and the Splashpress Media network every week, right in your Inbox. Relevant and timely content is yours for FREE!