Execute Multiple Custom Post Type Queries using WordPress Shortcodes

Add following snippet to “functions.php”

Snippet

add_shortcode('customPost', 'customPostType_shortcode');
function customPostType_shortcode()
{
extract(shortcode_atts(array(
        'type' => 'post',
        'limit' => '10',
        ),$atts));
 
        //The Query
    query_posts('post_type='.$type.'&showposts='.$limit);
 
        //The Loop
       if ( have_posts() ) : while ( have_posts() ) : the_post();
                echo  "<h3><a href=\"".the_permalink($post->ID)."\">".get_the_title($post->post_title)."</a></h3>";
                echo  the_excerpt();
        endwhile; else:
    endif;
 
        //Reset Query
    wp_reset_query();
}

Usage

[customPost type=movies,books limit=5]

Courtesy: GraphicBeacon

Reference

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