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'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!

share|improve this question

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

share|improve this answer
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 Jul 31 '11 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 –  Piet Jul 31 '11 at 14:48
    
Great, thank you so much for your help! –  Jayhal Aug 4 '11 at 14:08
up vote 0 down vote accepted

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();  
    ob_end_clean();  

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


    $post->post_content, $matches);  

    $first_img = $matches [1] [0];  
    if(empty($first_img)){ 
    //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.

share|improve this answer

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.