I had to display custom post type posts using short code and for this I have made research and got very nice code in which made some modification.
If anybody need to display custom post type posts using shortcode then you can use this code:
Just paste this code in functions.php
//custom post type function
function verdict_lists() {
register_post_type( 'verdicts',
// CPT Options
array(
'labels' => array(
'name' => __( 'Verdicts' ),
'singular_name' => __( 'Verdict' )
),
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields' ),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'verdicts'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'verdict_lists' );
add_shortcode( 'custom_posts', 'verdict_custom_posts' );
function verdict_custom_posts( $atts ){
global $post;
$default = array(
'type' => 'post',
'post_type' => '',
'limit' => -1,
'status' => 'publish'
);
$r = shortcode_atts( $default, $atts );
extract( $r );
if( empty($post_type) )
$post_type = $type;
$post_type_ob = get_post_type_object( $post_type );
if( !$post_type_ob )
return '
No such post type ' . $post_type . ' found.
';
//$return = '
' . $post_type_ob->name . '
';
$args = array(
'post_type' => $post_type,
'numberposts' => $limit,
'post_status' => $status,
);
$posts = get_posts( $args );
if( count($posts) ):
$return .= '
- ';
-
'. get_the_title().'
'. get_the_excerpt().'
foreach( $posts as $post ): setup_postdata( $post );
$return .= '
';
endforeach; wp_reset_postdata();
$return .= '
';
else :
$return .= '
No posts found.
';
endif;
return $return;
}
You can download code on word file also Click Here
Now on editor just paste this code:
[custom_posts type=’verdicts’]