Linking thumbnail to full size image

I use a function in my blog (getImage) to call thumbnails in the loop (it calls the 1st image on the post) but instead of link to the post, i would want the thumbnail to link to its full image src.

The function is this one:

function getImages($num) {
global $more;
$more = 1;
$link = get_permalink(); -> NO IDEA IF HAS ANY SENSE BUT I ALREADY TRIED TO CHANGE THIS ONE TO GET_ATTACHMENT BUT NO LUCK
$content = get_the_content();
$count = substr_count($content, '<img');
$start = 0;
for($i=1;$i<=$count;$i++) {
$imgBeg = strpos($content, '<img', $start);
$post = substr($content, $imgBeg);
$imgEnd = strpos($post, '>');
$postOutput = substr($post, 0, $imgEnd+1);
$postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"https://wordpress.stackexchange.com/", '/width="150"https://wordpress.stackexchange.com/",$postOutput);
$postOutput = preg_replace('/alignright/', 'aligncenter',$postOutput);
$image[$i] = $postOutput;
}
if(stristr($image[$num],'<img')) { echo '<a href="'.$link.'">'.$image[$num]."</a>"; }
$more = 0;}

Does anyone has some clue how can i do this? or other function to achieve this?

Thanks

2 Answers
2

Simply use wp_get_attachment_url($GLOBALS['post']->ID); instead of get_permalink();

Leave a Comment