1

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.

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

Jayhal
  • 27
  • 4

2 Answers2

1

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.

Jayhal
  • 27
  • 4
0

if your theme uses 'the_excerpt()' for the front page, I think you can add a filter in functions.php, and with a regexp change the link href from the download link to the permalink.

something like,

function replace_link($content) {
   if (is_home())
      return preg_replace('regular_expression', get_permalink(), $content);
   else
      return $content;
}
add_filter('the_excerpt', 'replace_link');

I can't create an actual regular expression without knowing what your download link looks like

yitwail
  • 1,999
  • 3
  • 20
  • 27
  • The links that point to an external url point to another page that hosts the files, and not to the actual files directly. – Jayhal Jul 10 '11 at 01:22
  • I'm using the 'Advanced Excerpt' plugin, would I add that php in the functions.php or would I need to edit the plugin files perhaps? Just asking to be completely sure. Thanks btw! – Jayhal Jul 10 '11 at 01:23
  • @Jayhal, it shouldn't matter where the links point to; as long as you can identify it, you can change it with the given code. I'm thinking you can just stick it into functions.php, but I'm not familiar with the plugin you're using. You're welcome...do me a favor & vote up my answer. :) – yitwail Jul 10 '11 at 01:57
  • I looked over the plugin. It uses the 'get_the_excerpt' hook. The snippet I provided uses 'the_excerpt' hook, which executes after 'get_the_excerpt'. I think that's what you want – yitwail Jul 10 '11 at 02:14
  • Great, thanx! This is a new account, it says I need 15 points to do that.. I will remember to come back though when I accumulate enough and hit that up for you :) – Jayhal Jul 10 '11 at 04:15
  • @Jayhal, my bad. I think you can accept my answer, by clicking the big check mark, even in you're new. And welcome to SO. :) – yitwail Jul 10 '11 at 05:03
  • 1
    Oh, apologies! Lol, I expressly remember clicking the check, my mistake. Thanks again. :) – Jayhal Jul 10 '11 at 13:21
  • Hmm, I'm trying to implement this now, and I'm not sure where inside the functions.php Ishould be placing this. Any help? – Jayhal Jul 31 '11 at 11:24