How to register shortcode for custom post types

Shortlink:

Today i will teach you how to register shortcode for custom post or your default post option. you can see below codes for register shortcode.

function post_shortcode($atts){
extract( shortcode_atts( array(
'count' => '5', // variable with default value if you need
), $atts) );

$q = new WP_Query(
array('posts_per_page' => -1, 'post_type' => 'posttype', 'orderby' => 'menu_order','order' => 'ASC')
);

$list = '<div class="post-area">'; // Your loop area before div.
while($q->have_posts()) : $q->the_post();
$idd = get_the_ID(); // for get dynamic post id
$custom_field = get_post_meta($idd, 'custom_field', true); // for get data from metabox
$post_content = get_the_content(); // for get content from content
$list .= '

// Your markup

';
endwhile;
$list.= '</div>'; // Your loop area close div
wp_reset_query();
return $list;
}
add_shortcode('wp-post', 'post_shortcode'); // wp-post is your shortcode

Your Shortcode wp-post

Example: If you will register post type is testimonial see below register new custom post codes.

function create_post_type() {
register_post_type( 'testimonial',
array(
'labels' => array(
'name' => __( 'Testimonial' ),
'singular_name' => __( 'Testimonial' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Testimonial' ),
'edit_item' => __( 'Edit Testimonial' ),
'new_item' => __( 'New Testimonial' ),
'view_item' => __( 'View Testimonial' ),
'not_found' => __( 'Sorry, we couldn\'t find the Testimonial you are looking for.' )
),
'public' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'menu_position' => 14,
'has_archive' => false,
'hierarchical' => false,
'capability_type' => 'page',
'rewrite' => array( 'slug' => 'testimonial' ),
'supports' => array( 'title', 'editor', 'custom-fields' )
)
);
}

add_action( 'after_setup_theme', 'create_post_type' );

You we can understand our post type is testimonial and if our markup is

<div class="post-area"
<div class="single_post_item">
<h2>This is title</h2>
<p>This is post content</p>
<p>Thi is custom metabox content</p>
</div>
</div>

then we can register shortcode within above html markup, you can see below codes.

function post_shortcode($atts){
extract( shortcode_atts( array(
'count' => '5', // veriable with default value if you need, input -1 for want to show all posts.
), $atts) );

$q = new WP_Query(
array('posts_per_page' => $count, 'post_type' => 'testimonial', 'orderby' => 'menu_order','order' => 'ASC')
);

$list = '<div class="post-area">';
while($q->have_posts()) : $q->the_post();
$idd = get_the_ID();
$custom_field = get_post_meta($idd, 'custom_field', true);
$list .= '
<div class="single_post_item">
<h2>'.get_the_title().'</h2>
'.get_the_content().'
<p>'.$custom_field.'</p>
</div>
';
endwhile;
$list.= '</div>';
wp_reset_query();
return $list;
}
add_shortcode('wp-post', 'post_shortcode');

 

You can mean:

get_the_title() // for get post title 
get_the_content() // for get post content
$custom_field // for get post metabox content

You can see Function Reference and get more get function for WordPress.

You to use this shortcode or display shortcode contents? you will use

<?php echo do_shortcode('[your shortcode]');?>

Example:

<php echo do_shortcode('[wp-post]');?>

If you face any problem when you will do it, also you can comments below comment box.

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!




%d bloggers like this: