Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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?

<?php
    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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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