0

I have created a shortcode to display the featured image linking to the post:

function ck13_post_image($atts){
   extract(shortcode_atts(array(
      'ck_img_size'      => 'full'
   ), $atts));

    $show_the_image .= '<a href="' . get_permalink() . '">' . the_post_thumbnail("$ck_img_size") . '</a>'; 
    return $show_the_image;
}

//---------------------------------------------

function register_shortcodes(){
   add_shortcode('show-post-img', 'ck13_post_image');
}
add_action( 'init', 'register_shortcodes');

But instead of displaying the image inside the post, the image is displayed above it.

The weird thing is that the link to the post is displayed correctly inside the post, just the image is above.
Did i make some mistake? How can i make this work?

0

the_post_thumbnail echoes its value, which doesn't work in variable assignment. Use get_the_post_thumbnail instead.

Most WordPress API functions follow this pattern, there are typically two versions - one which will echo the result and another with get_ prepended which will return the result.

1
  • Yeah, my bad here, i tried get_the_post_thumbnail just without $page->ID. Thanks.
    – CK13
    Apr 28 '14 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.