I'm working on a new Wordpress theme; the default index view displays the excerpts of recent posts. Some posts will be regarding file downloads, and include an image, description, and link to the location where the described files are hosted. The images for these types of posts will be anchored with links(other types of posts may contain images that are not linked).

For these types of posts, I would like the images to link to their entry's full post views(single.php) when displayed in excerpts, but for the same images to link to an external download link when displayed as part of the full post view.(The links that point to an external url point to another page that hosts the files, and not to the actual files directly.)

I'm not sure how exactly I would accomplish that. Any help would be greatly appreciated!

2 Answers 2


If I understand it correctly, in you index.php you want to link the image to the full post. You can accomplish that by adding this to your index.php:

<a href="<?php the_permalink() ?>" alt="<?php _e('Read full article', 'theme');?>" title="<?php _e('Read full article', 'theme');?>"><?php the_post_thumbnail('name-thumb'); ?></a>

assuming that you use the featured image to show the image on the index page

And in the full post (i.e. on single.php) you want the link to go to an external link. To accomplish this, I think you'd best add the external link during the image upload, where it says "image links to".

  • 1
    I do have the images linked to the correct url when posts are displayed, the main issue would be that the same images when viewed in index still link to an external site and not the full post. If I was not using tumbnails on my main page with the excerpts would my code then be: <a href="<?php the_permalink() ?>" alt="<?php _e('Read full article', 'theme');?>" title="<?php _e('Read full article', 'theme');?>">IMAGE URL</a> ?
    – Jayhal
    Commented Jul 31, 2011 at 14:43
  • oh ok, sorry for assuming that it was thumbs on the index page. Yes, as long as you link to the permalink, it will go to single.php
    – user2015
    Commented Jul 31, 2011 at 14:48

Because I don't have post thumbnails defined, and since posts may or may not contain multiple images, I ended up doing it this way:

I turned off tags in excerpts(I had an excerpt plugin enabling tags in excerpt), then I added the following to the function.php file:

function catch_that_image() {  
    global $post, $posts;  

    $first_img = '';  ob_start();  

    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', 

    $post->post_content, $matches);  

    $first_img = $matches [1] [0];  
    //Defines a default image    
    $first_img = "/images/default.jpg";  }  
    return $first_img;}

Then I added the following to the main index template file in between the posts title and the excerpt/content:

<p><a href="<?php the_permalink() ?>" alt="<?php _e('Read full article', 'theme');?>" title="<?php _e('Read full article', 'theme');?>"><img src="<?php echo catch_that_image() ?>"></a></p>    

Random note: It's wrapped in a

for styling reasons.

Thanx again for guiding me in the right direction.

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.