Retrieve and Display Images from a WordPress Post
I have already mentioned a few ways to do this but I have discovered a few problems with my old code. I have improved this one quite a bit so I felt that I should just delete my old suggestions and offer you the best technique I have discovered.
The whole point of this piece of code is to make it easier for people to retrieve images from their posts. If you want to pull only the excerpt but also want to display an image along side it, this code is all you need. Forget using custom fields or plugins, just add this code to your theme’s functions.php and then call the function within the loop to pull the image you want.
It functions by going through your post’s content and placing every image it encounters into an array. You can then use the function to call any image our of that array and display it.
If you already have a functions.php file in your theme’s folder, just place the following code between the opening <?php and closing ?> tags. If not, create one and make sure to add an opening <?php and closing ?> tag.
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; } |
Now whenever you want to display an image from a post, just use <?php getImage('1'); ?>. The number ’1′ can be changed to any number. If you want to pull the second image from your post, change it to ’2′ and so on. Be sure to place this within the loop so that is works properly.
NOTE: I just created a plugin for this called Simple Image Grabber.




I wonder if this code can be played with to pull a price in GBP (£) from the post?
Hello c.bavota! This is a great script that I used succesfully in a website – http://amordepapa.org – But now I was trying to resize the images with the TimThumb.php script and, as I’m new in PHP, I want to request your help to know how could I call the images this way:
So the question will be, how could I add this different code to the PHP? This: <img src="/scripts/timthumb.php? (..) and this: &h=150&w=150&zc=1"
This is the TimThumb website:
http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/
Thank you!
This script doesn’t work too great with the TimThumb script due to how it gathers the images. It gathers the whole img tag and outputs it. But here is some code that I use in most of my themes when I want to use the TimThumb script (the code below assumes your TimThumb script has been places in your current theme’s directory):
Then you can call the function using some variables like this:
So the following would display an image that is 200 by 120 pixels with a quality of 80, the class ‘alignright’ and no link:
By default, the class is alignleft, the quality is 80 and the showlink variable is true.
Please!!
I put your pugin script in my function.php file and I need so much use it with TimThumb. Could you help been clearer. The script you posted above do not work.
Please I need this too. I need to crop second, third, and fourth image to certain size, not to shrink it not resize than crop it with thimthumb.
Thanks in advance
Oh this is exactly what i am looking for my actress site, thanks friend thanks a lot for sharing.
But i would like display 3 imags side by side, i mean
my post have 12 images, out of those in the home page i need to display only 3 or 4.
then what ?
Fantastic plugin,
How would I get the first 3 images instead of 1. I’ve tried to repeat the code in index.php
‘
But that only will display the first 2 and repeat the second over again.
Thank you!
Fantastic plugin, (had to repost – forgot tags)
How would I get the first 3 images instead of 1. I’ve tried to repeat the code in index.php
But that only will display the first 2 and repeat the second over again.
Thank you!
Great stuff, been browsing around and found it all enjoyable. Selah
Hi C.Bavota! Please help me if you can. I’m try to using your code from comments#6922 and have error Parse error: syntax error, unexpected T_STRING in this line
.What that mean and how to fix that? Thank you!
You were missing a few periods. Try this:
THANKS! – just what I needed…
Great code… just what I was looking for.
However, I could only get it to display the first image from a post.
In order to get the other images using
and so on, I had to modify line #15 to be
.
The reason for that seems to be that
$imgEndis only the position in
$postand not in
$content. Adding
$imgBegmakes it the position in
$content.
A bunch of code was cutoff from your comment. Be sure to follow the instructions for placing bode between the pre tags.
Matthew – superstar, replacing line 15 with
fixes the code.
c.bavota – this is a great script, but without Matthew’s fix it only works for the first image.
Thanks all,
Ali
c, very impressive stuff. thanks, i have a question: is it possible to retrieve the original image URL. i think the call is
but i’m not sure how to hack that into my functions.php, i’d like to replace the .$link. with .$originalurl. or similar.
possible? point me in the direction, i’ll work it out i’m sure *cough* after days of googling.
thanks, beautiful website. congrats.
This is exactly what I was after. Thanks!
I did have a problem to start with and that was because I had inserted my image using NextGen Gallery shortcode rather than the WP default insert image. I went back to the posts and changed it and hey presto! Works like a charm.
Many thanks.
Is there a way to get the first images of the latest X number of posts with this snippet?
Thanks
If you use it within a loop where you specify the number of posts to query than yes.
Hi
i try both separately “plug-in” and “manual hack” both are good choice, but both have one major bug, manual hack did not resize image and plug-in resize internal image only, they did not resize external image.
i have 99% external images in my blog. please suggest something how i resize external image. i prefer manual hack.
Thanks
Brilliant!! and many thanks for sharing!
This is awesome man! Thank you for sharing, you rock!
Very helpful post! Thank you!
Good job! THANKS! You guys do a great blog, and have some great contents. Keep up the good work.
Hey,
Anyone experienced any problems with getting more than the second image? I’ve added , , but the third only seems to pull out the second image again. Any ideas where I have gone wrong down the line?
Any ideas would be much appreciated,
Rob
Opps, im a dummy, just noticed the post by Matthew! Many thanks to Matthew for making the code even stronger! good work guys!
Thank you so much! Its great to be able to slip this into your theme and not have to worry about it!
Great code & great post! I tried installing the plugin to WP v 2.9.2, It works! I suggest you resubmit/update `Simple Image Grabber`’s FYI/details. It says *Compatible up to: 2.8.4*. Well, thanks mate.
Cheers,
Nick
Hi,
How would I be able to getfetch the first image from a subpage.
Here is my code”
Very helpful post Thank you!!!
Hi, thanks for tutorial
but I want, in single post remove images and don’t show! can you help me?
Hi,Thank for your code
I very help full for me
Any way once again thanks