Display Custom Post Type with Short Code

WordPress is not just a blogging platform you can widely customize WordPress with your own different strategies or need. Default look provides you four things like Post, Pages, Categories, and Tags. You can create many posts to show in categories. What if we can create our own post types out of the box and show […]

WordPress is not just a blogging platform you can widely customize WordPress with your own different strategies or need. Default look provides you four things like Post, Pages, Categories, and Tags. You can create many posts to show in categories. What if we can create our own post types out of the box and show them according to our need anywhere. We are going to learn how to create custom post type and make post type short code.

Create Custom Post Type

So the first task is to create a new custom post type. Which will totally different from normal posts in WordPress they call it custom post type. That separate post type will make us able to make short code and can easily embed or display them in pages or posts.

add_action('init', 'portfolio_register');
function portfolio_register() {
$labels = array(
 'name' => _x('My Portfolio', 'portfolio'),
 'singular_name' => _x('Portfolio Item', 'portfolio'),
 'add_new' => _x('Add New', 'portfolio item'),
 'add_new_item' => __('Add New Portfolio Item'),
 'edit_item' => __('Edit Portfolio Item'),
 'new_item' => __('New Portfolio Item'),
 'view_item' => __('View Portfolio Item'),
 'search_items' => __('Search Portfolio'),
 'not_found' =>  __('Nothing found'),
 'not_found_in_trash' => __('Nothing found in Trash'),
 'parent_item_colon' => ''
 );
$args = array(
 'labels' => $labels,
 'public' => true,
 'publicly_queryable' => true,
 'show_ui' => true,
 'query_var' => true,
 'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
 'rewrite' => true,
 'capability_type' => 'post',
 'hierarchical' => false,
 'menu_position' => null,
 'supports' => array('title','editor','thumbnail')
 );
register_post_type( 'portfolio' , $args );
 }

Above code will add a custom post type tab in your sidebar of WordPress Admin. Then you can easily display it anywhere in your post or pages using the below shortcode called portfolios.

Display Custom Post Type with Short Code

    add_shortcode( 'portfolios', 'display_portfolio_post_type' );

    function display_portfolio_post_type(){
        $args = array(
            'post_type' => 'portfolio',
            'post_status' => 'publish'
        );

        $string = '';
        $query = new WP_Query( $args );
        if( $query->have_posts() ){
            $string .= '<ul>';
            while( $query->have_posts() ){
                $query->the_post();
                $string .= '<li>' . get_the_title() . '</li>';
$string .= '<li>' . get_the_post_thumbnail() . '</li>';
$string .= '<li>' . get_the_content() . '</li>';
}
$string .= '</ul>';
}
wp_reset_postdata();
return $string;
}

To display your Portfolio post type write in your post or page [portfolios] and it will display the post as desired. We just used <li> tags you need to write yourself what you want to. Have any problem or query regarding custom post types or short code then do write us we will help you.

One thought on “Display Custom Post Type with Short Code

Leave a Reply

Your email address will not be published. Required fields are marked *