0

By default, images included in an article link to the full-size-image url.

Having such an image in an excerpt shown on the start-page, because the more-tag was added after the image (which is intended), the image-thumbnail still links to the image URL.

Which action has to be taken to make it link to the article?
While keeping it pointing to the image url in the specific article-pages? (For my Fancybox)

Clarification: I don't mean Post-Thumbnails, just Images in the articles as shown in the specific article-page.

  • It might be helpful to paste the snippet of code you're currently using to display those posts on your start-page. – Jared Cobb Jun 1 '13 at 18:25
  • I am using the default Theme, Twenty Twelve. The images are uploaded and included in the article by the builtin media-manager. The excerpts are generated by adding the more-tag in the article. – iceteea Jun 1 '13 at 18:30
  • 1
    So the images need to point to the image URL on the single posts page but to the article on the index page? – s_ha_dum Jun 1 '13 at 18:32
  • Yes, that is what I want to achieve. – iceteea Jun 1 '13 at 18:37
0

Images are embedded in post content as pretty much straight HTML.

<a href="http://192.168.1.10/wp_release/wp-content/uploads/2011/01/boat.jpg">
  <img src="http://192.168.1.10/wp_release/wp-content/uploads/2011/01/boat.jpg?w=300" alt="" title="boat" class="size-medium wp-image-544" height="198" width="300">
</a>

There is no easy way to alter that. You can alter it, but it means regexing the post body content or using an html parser like Simple HTML DOM Parser (Not an endorsement, just an example).

The regex way would look something like this:

function replace_image_link_cb_101524($match) {
  global $post;
  if (!empty($match[2])) {
    $match[0] = preg_replace('|'.$match[1].'|',get_permalink($post),$match[0],1);
  }
  return $match[0];
}

function replace_image_link_101524($content) {
  if (is_archive()) {
    $pattern = '|<a.*?href="(.*)".*>?(<img.*?/?>)(?:</a>)?|';
    $content = preg_replace_callback($pattern,'replace_image_link_cb_101524',$content);
  }
  return $content;
}
add_filter('the_content','replace_image_link_');

That well could be buggy. regex on markup is tricky at best.

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.