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 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]*)"/', '/width="150"/',$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

share|improve this question
add comment

2 Answers

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

share|improve this answer
1  
Thanks, it doesn't work, it redirects me to the main page but i found a tricky (maybe messy solution). Using another function to catch the image url and instead the premanet_link i call the function to retriview the link :) –  nessie May 15 '11 at 13:35
1  
Could you add the function as an answer if it's the solution? Maybe others can use it later too. You should also try to put var_dump($GLOBALS['post']->ID); and var_dump(get-theID()); inside your function to test if you got all post data set up. Also take a look [here at the bottom of the page][1] to see the functions that get attachments. Maybe you can avoid your callback function completely. [1]codex.wordpress.org/Function_Reference/… –  kaiser May 15 '11 at 15:00
add comment

Oh sure, so here is what i did: I added this other function

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;
}

And I call it at the $link line, in the getimages function.

$link = catch_that_image();

Well is messy so i will try your suggestions and see if any of those will work, thanks a lot.

share|improve this answer
add comment

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.