I want to grab the two latest blog posts on my homepage. i would also like the option to select the category if I want to also, but mainly just pulling the latest 2 blog posts.



paste this code in your theme's functions.php file

function custom_query_shortcode($atts) {

   // [loop_shortcode the_query="showposts=100&post_type=page&post_parent=453"]

   // Defaults
      "the_query" => ''
   ), $atts));

   // de-funkify query
   $the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
   $the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);

   // query is made               

   // Reset and setup variables
   $output = '';
   $temp_title = '';
   $temp_link = '';
   $temp_ex = '';
   $temp_content = '';
   $temp_thumb = '';
   $temp_id = '';

   // the loop
   if (have_posts()) : while (have_posts()) : the_post();
      $temp_id = $post->ID;
      $temp_title = get_the_title($post->ID);
      $temp_link = get_permalink($post->ID);
      $temp_content = get_the_content($post->ID);
      //$temp_ex = get_the_excerpt();
        if ( has_post_thumbnail() ) {
        $temp_thumb = get_the_post_thumbnail($post->ID);
        } else {
        $temp_thumb = "" ;

      // output all findings -
     $output .= "<div class='post-$temp_id' id='post-$temp_id'>
                <h3 class='entry-title'>
                    <a title='$temp_title' rel='bookmark' href='$temp_link'>$temp_title</a>
                </h3><!--BEGIN .entry-content-->
                <div class='entry-content'>
                    <div class='theExrp'>
                        <a href='$temp_link'>

                    <div><a class='more-link' href='$temp_link'>read more...</a></div><!--END .entry-content-->
                </div><!--END .hentry-->
    endwhile; else:
      $output .= "not found.";
   return $output;
add_shortcode("loop_shortcode", "custom_query_shortcode");

then edit your home page and add this shortcode to show the two latest posts:

[loop_shortcode the_query="posts_per_page=2&orderby=date&order=DESC"]

and if you want to select the category the paste this short code:

[loop_shortcode the_query="cat=CHANGETHIS&posts_per_page=2&orderby=date&order=DESC"]

and replace the CHANGETHIS with your category id or name.

|improve this answer|||||
  • Wow, what an absolutely awesome answer. I've been meaning to learn how to use shortcodes because I believe it will make things easier for the client and your code works perfectly. – Dwayne Charrington Jan 16 '11 at 1:40
  • thanks, and you should learn how to use shortcodes and it does make things easier!!! – Bainternet Jan 16 '11 at 1:46

Well Kevin no one here is going to write code for you.

To make a shortcode you can use your functions.php

An example would be:

function hello() {

 return 'Hello, World!';


add_shortcode('yourshortcodename', 'hello');

In order to get posts based on date/category/etc you can find the parameters reference here http://codex.wordpress.org/Function_Reference/query_posts

|improve this answer|||||

if someone came here looking for the solution here is very nice plugin


|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy