By default, WordPress will link directly to an image in the category or post view. In a project I was working on today I wanted to change that. On the category view I wanted the image just to link to the post, and in the post, I didn’t want a link at all. Useful trick I found below:
function change_image_permalink($content){
$format = get_post_format();
//category listing page. link image to post
if (is_single() === FALSE AND $format == 'image'){
$content =
preg_replace(
array('{<a(.*?)(wp-att|wp-content/uploads)[^>]*><img}','{ wp-image-[0-9]*" /></a>}'),
array('<a href="' . get_permalink() . '"><img','" /></a>'),
$content
);
}
//post page. remove link
else if ($format == 'image'){
$content =
preg_replace(
array('{<a(.*?)(wp-att|wp-content/uploads)[^>]*><img}','{ wp-image-[0-9]*" /></a>}'),
array('<img','" />'),
$content
);
}
return $content;
}
add_filter('the_content', 'change_image_permalink');