Shortcode for Recent Posts

PLACE BELOW CODE IN FUNCTION.PHP
function my_recent_posts_shortcode($atts){
    $q = new WP_Query( array( 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => '4') );
    $list = '<ul class="recent-posts">';
        while($q->have_posts()) : $q->the_post();
            $list .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a>
            <br />' . get_the_date() . '<br />' . get_the_excerpt(). '</li>';
        endwhile;
    wp_reset_query();
    return $list . '</ul>';
}

add_shortcode('recent-posts', 'my_recent_posts_shortcode');
PLACE BELOW CODE IN PAGE
[recent-posts]
Or
<?php echo do_shortcode('[recent-posts]'); ?> in any page template.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s