Question

this is shortcode in template:

function shortcode_frame_left( $atts, $content = null)
{

   return '<span class="frame alignleft">'. do_shortcode($content) . '</span>';
}
add_shortcode('frame_left', 'shortcode_frame_left');

this is how using in post content:

[frame_left] <a href="YOUR-URL"><img src="YOUR-URL" /></a> [/frame_left]

I am trying to use this shortcode in template file for displaying post thumbnails. What i try:

if (has_post_thumbnail()) {

apply_filters( 'the_content', "[frame_left]".the_post_thumbnail()."[/frame_left]"); 
}

and

if (has_post_thumbnail()) {

$thumbnail = '[frame_left]'. the_post_thumbnail() . '[/frame_left]';
echo do_shortcode("$thumbnail");

}

What am i doing wrong?

Was it helpful?

Solution

the_post_thumbnail() echos the thumbnail and returns nothing. You probably want to use get_the_post_thumbnail() which returns it as a string. Your code currently is equivalent to this:

if (has_post_thumbnail()) {
    // Echo the thumbnail
    the_post_thumbnail();
    // Apply the filter but do nothing with the result.
    apply_filters( 'the_content', "[frame_left]"."[/frame_left]");
}

if (has_post_thumbnail()) {
    // Echo the thumbnail
    the_post_thumbnail();
    $thumbnail = '[frame_left]' . '[/frame_left]';
    // Echo the <span> with an empty content
    echo do_shortcode("$thumbnail");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top