Posted in Custom Post Type, wordpress

Display Custom Post Type on Page Using Shortcode By their Title, Content, Featured image etc

Display Custom Post Type on Page Using Shortcode

for this First install this plugin https://wordpress.org/plugins/display-posts-shortcode/ After this add function as mention below code:


add_shortcode('query', 'shortcode_query');

function shortcode_query($atts, $content){
extract(shortcode_atts(array( // a few default values
‘posts_per_page’ => ’10’,
‘caller_get_posts’ => 1,
‘post__not_in’ => get_option(‘sticky_posts’),
), $atts));

global $post;

$posts = new WP_Query($atts);
$output = ”;
if ($posts->have_posts())
while ($posts->have_posts()):
$posts->the_post();

// these arguments will be available from inside $content
$parameters = array(
‘PERMALINK’ => get_permalink(),
‘TITLE’ => get_the_title(),
‘CONTENT’ => get_the_content(),
‘EXCERPT’ => get_the_excerpt(),
‘COMMENT_COUNT’ => $post->comment_count,
‘CATEGORIES’ => get_the_category_list(‘, ‘),
// add here more…
);

$finds = $replaces = array();
foreach($parameters as $find => $replace):
$finds[] = ‘{‘.$find.’}’;
$replaces[] = $replace;
endforeach;
$output .= str_replace($finds, $replaces, $content);

endwhile;
else
return; // no posts found

wp_reset_query();
return html_entity_decode($output);
}

After adding above Code in functions.php add new attribute into the shortcode : post_type
Shortcode Example is : [query post_type=job posts_per_page=-1]

Advertisements

Author:

I am Wordpress Developer.

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