Mar
11
2009

Simple Image Grabber WordPress Plugin

by   |  Posted in Downloads  |  145 comments

I created a plugin for the function I talked about in my post Retrieve and Display Images from a WordPress Post just in case there is anyone out there who isn’t a fan of hacking code. Just install this sucker and use the following function:

<?php images($number, $width, $height, $class, $link); ?>

Make sure it is within the loop and it will display whichever image you want.

$number = the image you want to pull from your post, ie. the first image from the post (’1′) or the second image from the post (’2′) and so on. NOTE: If you use ‘all’, it will display all images from the post.

$width = the width of the displayed image

$height = the height of the displayed image

$class = the class name you would like to assign to the displayed image

$link = whether you would like the displayed image to link to the post or not

So, the following function:

<?php images('2', '150', '200', 'alignleft', false); ?>

would display the second image from a post (if there is one) with a width of 150px and a height of 200px, the class name alignleft and no link to the post.

The following:

<?php images('all', '', '', 'alignright'); ?>

would display all images from a post with their original width and height, a class name of alignright and a link to the post.

View Changelog

1.0.2 (2009-10-15)
  • Added original width and height to img tag if neither is set
1.0.1 (2009-03-16)
  • Fixed issue with "All" variable
1.0 (2009-03-11) Initial Public Release

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
Share the love...
If you require help or support, please visit the Support Forum and ask all your questions there. Questions about themes or plugins posted in the comments below will not be answered.

Tags: , , , , , , , , , , , , ,

Short URL: http://bit.ly/bF2qfT

Discussion 145 Comments

  1. brendan on April 16, 2009 at 5:52 pm

    also, is there any way to have the plugin place a default image if there is no image within the post?

    • c.bavota on April 23, 2009 at 11:38 am

      Let me see if I can add these options in a newer version.

  2. Frank on April 17, 2009 at 5:00 pm

    Mr. Bavota…

    Thank you very much for answering my question! I will try this right away!

    yours gratefully!

    cheers!

    Frank

  3. Frank on April 20, 2009 at 11:25 pm

    Dear Mr. Bavota,

    Thank you for your help-your instructions worked perfectly!

    I do have an alternative website design which has both an index page and home page. It seems the home page is actually governing the layout, and it offers a default thumbnail. I can’t figure out where you code should go. I am pasting a clip of the code here if you wouldn’t mind pointing out where I should place your code?


    Title

    have_posts()) : $recent->the_post();?>
    ID, “thumbnail”, true) ): ?>
    <a href=”" rel=”bookmark”><img style=”float:left;margin:0px 10px 0px 0px;” src=”ID, “thumbnail”, true); ?>” alt=”" />

    <a href=”" rel=”bookmark”><img style=”float:left;margin:0px 10px 0px 0px;” src=”/images/thumbnail.png” alt=”" />

    <a href=”" rel=”bookmark”>

    Read More Posts From This Category

  4. Frank on April 24, 2009 at 1:19 am

    Hi everyone,

    I am so sorry to ask, but I am not a programmer at all. My theme has a generic thumbnail in the code, and I am having a real hard time with this trying to find where to put the code, and trying to figure out how to, or even if, I should remove the thumbnail code. Is there someone who could give me one or two pointers please?

    cheers!

    Frank

  5. CHM on May 11, 2009 at 7:14 pm

    Is it possible to crop the image contextually instead of resizing it out of proportions??

  6. CHM on May 11, 2009 at 7:24 pm

    Hmmm never mind ;) I forced it with CSS

    Giving the class a “display:inline-block” and then specifying the heigth + width.
    Thanks for this great plugin

  7. Jon Eland on May 13, 2009 at 6:29 pm

    Great idea – would be brilliant use to me if…

    It chose images from the Media manager Gallery, not the post
    You could specify it to use the thumbnail WordPress generates, rather than a scaled version of the final image.

    Otherwise – great.

    • c.bavota on July 27, 2009 at 7:51 am

      there is a great script out there called timthumb that resizes the image using PHP’s GD library. It works great.

  8. Wayne on May 16, 2009 at 2:40 pm

    Hello c.bavota, this is a fantastic plugin. One question though, is there any way to get the thumbnail to be of a particular size ie 150×105 but retain aspect preportion so the image is not squashed?

    • Wayne on May 16, 2009 at 2:44 pm

      Also you look like you know a thing or 2 about wordpress. Can u plz tell me what plugin http://www.lifehack.org/ is using to display there “latest topics articles”? thanks again.

    • pixelMauler on February 12, 2010 at 12:17 pm

      I usually specify either the height or the width, never both for client sites. To keep the squashed look from ever happening.

  9. Vietmanga on May 19, 2009 at 10:09 am

    nice plugin. And would be great if you make a plugin limit posts automatically with a image on font page.

  10. Vietmanga on May 19, 2009 at 10:25 am

    A problem i meet. If a image from another host, and there is a link under it, the image will not display in right size.

  11. ivan on May 20, 2009 at 2:47 pm

    Hello…

    When I add a new article to my blog I add a main image to every post, of course the image also will display when viewing a single post.

    What i want is to show the attached image only on the main page post and not on a single post.

    Please tell me the way.

  12. vinoth on May 23, 2009 at 4:16 am

    Could u please tell me, shall we use this function to retrieve images and display it for recent post (Recent Post with thumbnail).. if so can u explain me how?

  13. vinoth on May 23, 2009 at 4:22 am

    Also at the same time If i put the image with align centre tag in the post, in the home page it doesn’t display the image in the left. it always display the image as it is aligned in post. you can check that in my blog too.. Please reply me my friend..

  14. stephane on June 4, 2009 at 4:51 am

    Dear M. Bavota,

    First, thank you very much for this good plugin. It works well for me.(wordpress 2.7)

    Otherwise, I think it could be great to have a link to the image and reach the article when we click on.

    Is it possible to do this?

    Thanks a lot for your help,

    Kind regards,

    Stephane

  15. Jojo on June 7, 2009 at 6:12 pm

    How can I grab the ALT text of the image?

  16. Denis on June 17, 2009 at 6:12 am

    This is great.. I always used custom fields in my themes which are great also but now suck!!! coz this rocks!!! thanks alot.

  17. alex on June 25, 2009 at 10:23 am

    Thanks, excellent article.

  18. Juarez P. A. Filho on July 8, 2009 at 2:51 pm

    Hey there… This plugin is AWESOME, but I tried use it inside a WP_Query loop and I have no success.
    Could you help me please.
    http://gist.github.com/143079

    Thanks

  19. Chris on July 23, 2009 at 4:12 pm

    Great work, for the non plugin version of this, is there a way to display a generic image if there is no first image, Im using it for a list you see.

    Thanks in advance

    • c.bavota on July 27, 2009 at 7:53 am

      You would have to throw an if statement in there looking for a value in the variable. If there is a value it would display the image, if not it would display a generic image.

  20. Jo on July 29, 2009 at 8:40 pm

    I love the plugin… very clean and simple.

    But I am looking to make the link to the image file. Is that possible? This is an image drive blog and I want to posts to show the first image. But when you click on it, it will open the file in shadowbox. I am hoping that the other images in the post will be in the shadowbox gallery with it, but I might be hoping for something that can’t happen.

    Is there a way to at least link to the targeted image file? I know you are dynamically targeting the src, just figured I could write the href the same way.

    Thanks,
    Jo

    • Daniel on September 7, 2009 at 4:20 pm

      Did you ever find a solution for this? I’d like to do the same thing?

      d

  21. Edward on August 5, 2009 at 3:45 pm

    Hello,
    Is there a way to embed the correct proportions(height and width) of the first image, as opposed to custom. I need this for my javascript to run properly as soon as DOM is ready, but at the moment it only works on second reload, when all the images are already in the cache.

    It would be even nicer if I could have such a fix inside the non-plugin version of this script.

    Many thanks.

    • Edward on August 7, 2009 at 7:00 pm

      Anybody at all?

    • Andrea on October 14, 2009 at 4:39 am

      Great plugin, but I have the same problem as Edward up here. I need the height and width of the image picked to be specified in the html code. Any help?

    • c.bavota on October 15, 2009 at 9:58 am

      Version 1.0.2 fixes that issue. It should be available on WordPress shortly.

  22. Mantasha Zaara on September 13, 2009 at 3:21 pm

    It worked gr8 for me… thanks alot got your gr8 plugin…I used code for center alignment..

    thanks once again

  23. Hampus on September 24, 2009 at 11:11 am

    Hi there!
    I’ve installed your plugin and it all works as a dream!
    The only problem I experience is that the remaining images in the post is still diplayed together with the duplicated first image. So the 3 images in my post becomes 4 (first imagex2)

    If anyone could help me to solve this problem, it would be hugely appreciated!

    Best,
    Hampus

  24. Hampus on September 24, 2009 at 12:41 pm

    I forgot to mention that I’m talking about the homepage here. Basically I want to hide all images in the posts and just use this script to show the first one for each post. Is this possible? Seems as it is, I just need help to figure out how to hide the remaining images.

    I read somewhere that the you can set to only show the post excerpt. Would this work?

    Thank you in advance!

    • c.bavota on September 24, 2009 at 2:12 pm

      If you use the_excerpt() instead of the_content() all the images will be stripped from your content and you can then use the image grabber to display the image you want.

    • Hampus on October 12, 2009 at 3:24 am

      When I’ve changed the content to the excerpt the links in the post doesn’t work, any idea why?

      Thanks!

    • Hampus on October 12, 2009 at 3:29 am

      The image grabber and all works great but not the links. changing back to content shows the links but doubles the images…

      code looks like this:
      <?php the_excerpt('’.__(‘Continued reading >’, ‘simplr’).”); ?>

      might be something wrong with that… dunno

  25. Hampus on September 24, 2009 at 11:17 pm

    That works perfectly!!! I’m so thankful for your help, very much appreciated!

    One last thing would be how to crop the height of the homepage images to 300px. with the image centred in the frame.

    Any suggestions? Read about a couple of ideas here but just want to check with you what you think might be the best solution for my case.

    Thanks heaps!