How to pull blogposts using shortcodes

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

Related posts

Leave a Reply

3 comments

  1. 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.