How to add a user defined hyperlink to the “Featured Image” and the_post_thumbnail()

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>'); ?>

Related posts

Leave a Reply

2 comments

  1. 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.

    EDIT:

    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().

  2. 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.

    <?php 
      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 */
    
      }
    ?>