Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

Thanks

share|improve this question

3 Answers 3

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

function custom_query_shortcode($atts) {

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

   // Defaults
   extract(shortcode_atts(array(
      "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               
   query_posts($the_query);

   // 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'>
                    <p>
                        <a href='$temp_link'>
                            $temp_thumb
                        </a>
                    </p>
                    <p>$temp_content

                    </p>
                    </div>
                    <div><a class='more-link' href='$temp_link'>read more...</a></div><!--END .entry-content-->
                </div><!--END .hentry-->
            </div>";
    endwhile; else:
      $output .= "not found.";
   endif;
   wp_reset_query();
   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.

share|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. –  DigitalSea 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

share|improve this answer

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

http://wordpress.org/extend/plugins/display-posts-shortcode/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.