SIMPLE RECENT POSTS SHORTCODES FOR WORDPRESS

(Update – 21st February, 2012 – I’m no longer using this on my site, but the method still works)

Over the last couple of days I have been working on updating my site’s theme. More specifically, I have been converting it to a Hybrid child theme.

I created my original theme from scratch, and it wasn’t particularly brilliant in terms of construction. I basically had a separate template for every page. I somewhat missed the point, methinks. I have leaned a lot about WordPress development since then, so decided it was time for an update.

I wanted a simple way of adding the various bits and bobs to the homepage (recent posts, flickr images, twitter) that didn’t involve hacking about with template files. Here’s what I did for the recent posts section:

Really Simple

There’s a handy function built into WordPress: wp_get_archives(). A first glance, it doesn’t look like it’s going to be very helpful for displaying recent posts, but by passing it a few arguments, it becomes very helpful indeed. All the possible arguments are explained on the Codex, but the interesting ones are:

type – setting this to postbypost means the function retrieves posts ordered by date
limit – the number of posts to retrieve
echo – this should be set to 0 (false), for reasons explained below
So, to retrieve the 5 most recent posts, I used the function as follows:

wp_get_archives( 'type=postbypost&limit=5&echo=0' );
To output this where I wanted, I could have just dropped it into the template file in the appropriate place, but as mentioned earlier, this is what I want to avoid. So I made use of the WordPress Shortcode API. It’s really easy to use. I won’t explain the basics of shortcodes here, the Codex entry explains it well.

Simply adding the code below to my theme’s functions.php allowed me to use the shortcode [recent-posts] wherever I liked.

function my_recent_posts_shortcode() {
return '

    ' . wp_get_archives( 'type=postbypost&limit=5&echo=0\n\r\c' ) . '

';
}

add_shortcode( 'recent-posts', 'my_recent_posts_shortcode' );
Notice that I manually wrap the output of wp_get_archives() in a

    , the function doesn’t do this for you. The reason for echo=0 can be seen here too: the list needs to be returned, not directly echoed, so that it is output in the correct spot.

    I also added a class attribute the the

      , so that I could style it as I liked.

      This functionality could be extended slightly, by using more features of the Shortcode API, which would allow us to define the limit argument of wp_get_archives() in the shortcode itself, rather than hardcoding it. Like so:

      function my_recent_posts_shortcode( $atts ) {
      extract( shortcode_atts( array( 'limit' => 5 ), $atts ) );

      return '

        ' . wp_get_archives('type=postbypost&limit=' . $limit . '&echo=0') . '

      ';
      }

      add_shortcode( ‘recent-posts’, ‘my_recent_posts_shortcode’ );
      The shortcode could then be used like this:

      [recent-posts limit=”3″]
      Where the limit attribute is the number of posts to retrieve.

      This shortcode could be used on a post, a page, or even in a Text widget.

      More Complicated, but Flexible

      The above is great and works well, but I decided that I wanted a bit more customization of the output, specifically the ability to show the publish date of each post. This required a different approach. Here’s the code to be added to functions.php

      5 ), $atts ) );

      $q = new WP_Query( 'posts_per_page=' . $limit );

      $list = '

        ';

        while ( $q->have_posts() ) {
        $q->the_post();
        $list .= '

      • ' . get_the_date() . '' . get_the_title() . '' . '
      • ';
        }

        wp_reset_query();

        return $list . '

      ';
      }

      add_shortcode( 'recent-posts', 'my_recent_posts_shortcode' );
      ?>

      As you can see, the shortcode stuff remained the same. The wp_get_archives() function has been replaced with a custom query, which allowed me to build up the list markup however I required.

      The value of the limit attribute from the shortcode is passed to the WP_Query object as the value of its posts_per_page parameter. This limits the number of posts retrieved in the same way as before.

      The while loop simply goes through the posts returned by the query, generating the markup, which could of course be customized in any way.

      wp_reset_query() is called to ensure that this doesn’t mess up the main WordPress loop, or other custom loops that may be processed later.

      There are a multitide of possible parameters that can be passed to WP_Query, so this could easily be adapted to retrieve posts from a certain category, or from all categories except one, or from a specific author, or whatever is required.