I am using this code in my wordpress Functions file to automaticly find the first image from the post content. function getImage($num) { global $more; $more = 1; $link = get_permalink(); $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]*)"/', '',$postOutput);; $image[$i] = $postOutput; $start=$imgEnd+1; } if(stristr($image[$num],'<img')) { echo '<a href="'.$link.'">'.$image[$num]."</a>"; } $more = 0; } Code (markup): It's from http://bavotasan.com/tutorials/retrieve-the-first-image-from-a-wordpress-post/ I use <?php getImage(1); ?> Code (markup): And it outputs the full <img code from the post. What i need to do is just show the src or url of the image. I have this code that does that but not sure how to get them to work together or just edit the main code so it outputs the image src instead of full image tag. $image = '<img src="http://something.com/someimage.jpg" align="right" vspace="5" hspace="5" border="0" class="img">'; // get the src for that image $pattern = '/src="([^"]*)"/'; preg_match($pattern, $image, $matches); echo $matches[1]; Code (markup): Can anyone help ? 10$ for first person to answer.
what version of wordpress are you using? let me know and I'll download that to test the code with that before re-posting it here.
Already Tried that one form http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it but it always just displays the default image for me. The first code works fine and ouputs the image, but i just want it to show me the src url instead of image tag.
i have replay, hope you found solution... Problem not on code above, but how you call image on single post...
The image is not called separately, it's within the post content. The only difference it it doesn't uses default wordpress classes. The first code outputs the image fine.