I want to wrap a link around a featured image (only if the custom meta box has been filled in), the link will come from a custom meta box.

Here's my image code: the_post_thumbnail('screenshot');

Here's my code for displaying the link using text:

<?php //Only if $urllink is filled in, display this link. ?>
  <?php $urlbox = get_url_desc_box();
    if ( !empty( $urlbox[0] ) ) {
      echo sprintf( '<a target="_blank" href="%s">Visit Website &raquo;</a>', $urlbox[0] );
} ?>

I've tried a few ways to get this to work but can't figure it out.

Any help would be greatly appreciated.

1 Answer 1


Unless there's some part of your problem I don't grasp, what's wrong with a simple mash-up of the two given snippets?

    global $post;
    $urlbox = get_url_desc_box();
    $post_thumb = get_the_post_thumbnail( $post->ID, 'screenshot' );
    if( !empty( $urlbox[0] ) && !empty( $post_thumb ) ) {
        echo sprintf( '<a target="_blank" href="%1$s">%2$s</a>', $urlbox[0], $post_thumb );

Edited: get_the_post_thumbnail vs. the_post_thumbnail

In accordance to the general WP function naming convention, the_post_thumbnail echoes/displays the image, whereas get_the_post_thumbnail returns it.
In addition to that, the_post_thumbnail can only be used inside the Loop, while get_the_post_thumbnail takes one further parameter, the post ID. The two optional paramters after the ID are identical to those of the_post_thumbnail.

Hence, you can use get_the_post_thumbnail( $post->ID, 'screenshot' ) to preserve your size settings.

  • I've tried your code snippet above Johannes, but the image does not appear, the link does if you peek at the source code. Oct 1, 2012 at 8:57
  • Sry, didn't check the nature of get_the_post_thumbnail --> Needs to be fed the post ID. Edited. Oct 1, 2012 at 9:01
  • Image now displays, thank you. But it's not at the set width & height. 'the_post_thumbnail('screenshot');' Oct 1, 2012 at 9:13
  • See 2nd edit. get_the_post_thumbnail also takes the $size paramter. Oct 1, 2012 at 9:23
  • Fantastic, thank you so much for your help. Works a treat. Thank you. Oct 1, 2012 at 9:31

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.