0

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?

3
  • 2
    Your shortcode definition is frame_left, code examples use frame_right. Can I assume you have frame_right shortcode defined in same way?
    – Rarst
    Commented Nov 15, 2010 at 7:18
  • OH so sorry, that was example, i copy wrong shortcode seems, template have both frame_left and frame_right btw. Commented Nov 15, 2010 at 14:53
  • @Ünsal: You don't have to edit your question to include the answer, that will confuse future readers. Just leave it as it was, so people with a similar problem can find the question and the given answer(s). I recommend you edit it and the rollback to the previous revision.
    – Jan Fabry
    Commented Nov 15, 2010 at 17:02

1 Answer 1

4

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");
}
4
  • not working.. for example your 2nd codes result is: <img src=""><span class="frame alignleft"></span>[/frame_left] Commented Nov 15, 2010 at 16:39
  • image is displaying but not inside of <span> and there is [/frame_left] which is weird Commented Nov 15, 2010 at 16:40
  • oh i got it.. you mean answer is if (has_post_thumbnail()) { // Echo the thumbnail $thumbnail = '[frame_right]' . get_the_post_thumbnail() . '[/frame_right]'; // Echo the <span> with an empty content echo do_shortcode("$thumbnail"); } Commented Nov 15, 2010 at 16:44
  • @Ünsal: Indeed, my example is not what you should do, but what your current code is doing. You should just use your current code, but replace the_post_thumbnail() with get_the_post_thumbnail() (and probably echo that apply_filters() result?).
    – Jan Fabry
    Commented Nov 15, 2010 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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