Get posts by id using shortcode

I am trying to get posts by id using shortcode [testimonials id=”272″] in functions.php file. I used the below code it is working but not able to get posts by id. Can anyone help me with this issue if I have done Wrong?. Thanks in advance.

add_shortcode(‘testimonials’, ‘testimonial_query’);
function testimonial_query($atts, $content){
extract(shortcode_atts(array( // a few default values

‘id’ => null,
‘posts_per_page’ => ‘1’,
‘caller_get_posts’ => 1)
, $atts));
$args = array(
‘post_type’ => ‘testimonial’,
‘numberposts’ => -1
global $post;
$posts = new WP_Query($args);
$output = ”;
if ($posts->have_posts( $attributes[‘id’]))
while ($posts->have_posts( $attributes[‘id’])):
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), ‘thumbnail’ );
$out = ‘<div class=”testimonial-img”>
<img src=”‘.$url.’” />
<div class=”testimonial_content”>
<img src=”‘.get_field(‘logo’).’” alt=”icon”>
<p class=”testimonial_desc”>’.get_the_content().'</p>
<div class=”author-details”>
<img src=”‘.get_field(‘author_image’).’” alt=”image”>
<p>’.get_field(‘author_name’).’ <span>’.get_field(‘author_designation’).'</span></p>
// add here more…
$out .='</div>’;
/* these arguments will be available from inside $content
get_the_category_list(‘, ‘)
and custom fields
get_post_meta($post->ID, ‘field_name’, true);
return; // no posts found

return html_entity_decode($out);

Read more here:: Get posts by id using shortcode

Leave a Reply

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