I've enabled my theme with the ability to display the "Featured Image" for the post. However, I'm trying now to determine (1) How to assign a hyperlink to the image and (2) How to call the_post_thumbnail() so that it wraps the hyperlink around the image.

I can't find where this is supported in the current release of WP, but I'm thinking that I must just be missing something.

<div class="entry">
<?php if(has_post_thumbnail() && get_option('theme_show_featured_image'))
      the_post_thumbnail('large', array(
     'class' => 'alignleft', 
     'style' => 'margin:0 10px 10px 0;')); ?>
<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>

I assume you want this to be output inside of the_content()?

You would probably have to define a shortcode that will output the_post_thumbnail(). You could either define the shortcode to accept a URL as an argument, or else wrap the shortcode with HTML anchor tags.


Assuming you're already outputting the_post_thumbnail() inside of the_content(), you could add a custom field into which the user enters a URL, and then, if the URL is present, output anchor tags around the call to the_post_thumbnail().

  • Yes, Ive got it displaying fine, just need to add a custom, user-defined hyperlink option. – Scott B Apr 27 '11 at 2:43
  • What about using a custom field? (See answer edit.) – Chip Bennett Apr 27 '11 at 3:22

Use get_the_post_thumbnail() function for getting the featured image. This will return the img string for you, instead of echoing it immediately.

Also, add a Custom Field to the post, named, for example URL-for-featured-image, and add the URL you want the image to link to as the value. Get it's contents with get_post_custom_values('URL-for-featured-image') and use the result in the hyperlink's href.

  if(has_post_thumbnail() && get_option('theme_show_featured_image'))
    $img = get_the_post_thumbnail($post->ID, 'large'); 
    $url = get_post_custom_values('URL-for-featured-image');
    /* your code here */

  • thanks for the suggestion. Definitely will be adding a custom field since I now know there is no built in option to use a hyperlink for the featured image. Seems an oversight to me that its not there. Every click map I've seen tells me people click on images like mad, whether they are linked or not. – Scott B Apr 28 '11 at 17:17

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.