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
If you liked this, please share it.
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. Adriaan on March 12, 2009 at 5:47 am

    Hi thanks for this, working like a charm. Dont know why people still bother with those post image plugins.

    Just one question, how would I go about wrapping the text next to the image. Floating?

  2. Adriaan on March 12, 2009 at 5:51 am

    Nevermind, I just added this to me css

    .alignleft{
    float:right;
    padding:5px 5px 5px 5px;
    margin-left:3px;
    background-color:#e8e8e8;
    border:1px solid #bed0ec
    }

  3. baron on March 12, 2009 at 1:06 pm

    Works great, thank you

  4. Free Call World Wide on March 12, 2009 at 8:17 pm

    Good Post.Thanks:-)

  5. Ani on March 14, 2009 at 12:14 pm

    I like this plugin and I tried just it.
    Now I wonder why there is shown picture one twice, and picture two isn’t shown at all! What did I wrong?

  6. Li-An on March 14, 2009 at 5:07 pm

    Hello,
    As I’m using Post Thumb Extented plugin, I’m interested in your plugin. Does it resize the image or only force the image to be viewed with preselected dimension ?

    • c.bavota on March 16, 2009 at 5:29 pm

      It uses CSS to resize the image, it doesn’t actually resize the file.

  7. Johannes on March 15, 2009 at 9:41 am

    Nice plugin! It has a bug though: If you use ‘all’ with more than one picture in the post, the plugin outputs the first picture x times (x being the number of pictures in the post). To fix this, you need to change line 29 from “$start=$imgEnd+1;” to “$start = $imgBeg + $imgEnd + 1;”. That’s necessary because $imgEnd is not counted from the beginning of the post content but from $imgBeg.

    • Ani on March 15, 2009 at 1:01 pm

      Thank you so much. =)

    • michael on January 16, 2010 at 12:18 pm

      Johannes:

      Thank you, now I can put the first 1,2,3 images.

    • jtmkrueger on December 21, 2010 at 5:06 pm

      YES! this makes it work, thanks Johannes!

  8. Ani on March 15, 2009 at 1:45 pm

    Can I also show only Pic 1, 2 and 3 (if I have 4 or more)?
    How can I do that?

  9. pupungbp on March 15, 2009 at 9:28 pm

    Nice Plugin, really helpfull… I will use this plugin in my next wordpress theme….

  10. paul on March 19, 2009 at 1:32 am

    Hey,I can’t get this to work. I just want it to loop through my posts and display the first thumbnail image. Right now my posts only have 1 image. When I put the following code, no image shows up. What am I missing?

    <?php the_content(‘Read the rest of this page »’); ?>

    <?php
    $postslist = get_posts(‘numberposts=8&category_name=’);
    foreach ($postslist as $post) :
    setup_postdata($post);
    ?>

    <a href=”">View Video

    • paul on March 19, 2009 at 1:35 am

      It cut me off,
      here is the part that should matter

      <?php
      $postslist = get_posts(‘numberposts=8&category_name=’);

      foreach ($postslist as $post) : setup_postdata($post);?>

      <a href=”">View Video

    • paul on March 19, 2009 at 1:40 am

      sorry for the volume of comments, it is trying to execute the php parts…

      <![CDATA[
      <?php
      $postslist = get_posts('numberposts=8&category_name=');
      foreach ($postslist as $post) : setup_postdata($post);?>

      <a href="">View Video

      ]]

      …hopefully it works this time

    • paul on March 19, 2009 at 1:56 am

      ok, i realize the issue i had.

      how can i show the first image in the post media library instead of in the actual post.

      i need to show a video still on one page and have the actual video on another.

    • c.bavota on March 20, 2009 at 8:45 am

      I don’t think you can actually display the images from the media gallery unless they are actively added to your post. Let me look into this though, and try to get back to you.

    • c.bavota on March 21, 2009 at 8:15 am

      I just wrote a post on Displaying the Post’s Gallery in WordPress that might help you out.

  11. M.M. Turner on March 21, 2009 at 7:57 am

    Will this work on the single.php too or only in the loop? I’m looking for a solution to display the image on top of the title and the text on the single page. thanks

    • c.bavota on March 21, 2009 at 8:16 am

      It’ll work on single.php but it needs to be within the loop.

    • M.M. Turner on March 21, 2009 at 8:53 am

      thanks for your answer. to be honest I’m quiet new to wordpress… I thought your plugin might be a good way to change the order from tile>image>text to image>title>text. but now the image is show twice of course. is there a way to exclude the original image in the single page? thanks for your help!

    • c.bavota on March 23, 2009 at 4:15 pm

      Add this piece of code to your functions.php file to strip all the images from your content.

      function theme_content($readmore = null) {
      $content = get_the_content($readmore);
      $content = preg_replace('/]+./', '',$content);
      $content = apply_filters('the_content', $content);
      $content = str_replace(']]>', ']]>', $content);
      echo $content;
      }

      The use theme_content(); instead of the_content() on your single.php page.

    • M.M. Turner on March 28, 2009 at 5:10 pm

      Thanks, works perfectly!

      I found this on an other site and it works great as well:

      <?php
      $filter = ‘#]*>#iU’;
      $neuer_content = preg_replace($filter, ”, get_the_content());
      echo $neuer_content;
      ?>

  12. sweety on March 21, 2009 at 8:16 am

    great work dude thanks alot

  13. Aaron on March 23, 2009 at 2:17 am

    I’ve been trying this for the last hour, and still no luck. This is my code:

    <div id="post-">

    The tags are within the loop, and I tried deactivating any other plugins that might conflict, but still no luck. It’s just not showing anything.

    I definitely have three images attached to the post.

  14. Aaron on March 23, 2009 at 2:57 am

    Do the images actually have to be in the post for this to work, or just attached in the media gallery?

    • c.bavota on March 23, 2009 at 4:17 pm

      This will only display images added to the post directly, not just to the post’s gallery. I am going to update the next version to have this option.

  15. Pablo on March 23, 2009 at 7:56 pm

    i want this plugin but for the links
    any one have idea?

  16. bass-t on March 27, 2009 at 10:21 am

    Hey, great plugin works great on my site……! Thank you for that!
    But now I wonder if it is possible to add links to the images,
    would be nice to make them clickable and to open them in a new window (original sized).
    similar to the wp gallery function….!
    Perhaps you have an idea.

  17. Pete on April 1, 2009 at 5:57 am

    Wow,

    this is a very useful plugin!

    I’m wondering if there is a way to preserve the proportion of width & height as images get distorted if you don’t put in the exact proportions of the original – I think it already works on your Magazine Basic Theme with the excerpts of articles that contain images…

    Pete

  18. Pete on April 1, 2009 at 6:54 am

    ok,
    I got it: you can just leave width & height options blank and style via css… that works best if you’re willing to cater a special version to IE…

  19. Dex on April 1, 2009 at 7:37 am

    hello…i am just new to this very wonderful theme. I don’t know how to figure out the photos in the homepage.but let me check on this..i’ll come back again if i can’t sort this out

    • c.bavota on April 1, 2009 at 10:16 am

      Just add your pics to your posts and they will appear on the main page. But you need to add them to the post and not just to the gallery.

  20. Dex on April 1, 2009 at 10:49 pm

    anybody could please help me where in my theme shoud I paste the following. Is it in index.php?

    • c.bavota on April 2, 2009 at 12:16 am

      Hey Dex,

      Paste the code in the index.php file. That should do it.

  21. Dex on April 1, 2009 at 10:59 pm

    Hello C. Bavota,
    I am not sure what i am doing in my template. If you check my site at http://www.pinoybro.com it seems the image are making the post distorted. I already downloaded the plugin simple image grabber and the next thing i need to know is where in the php should i put the following code <?php images(’2′, ’150′, ’200′, ‘alignleft’, false); ?
    to adjust the images. Is it at the index.php or in the single.php?

    Thank you very much,
    Dexter

  22. Edo on April 6, 2009 at 4:36 am

    Thanks for the great plugin. I tried many plugins before, but this is the best!
    Only one problem: if the image width and height defined in the html code, then the plugin cannot resize the image. It doesn’t happen often, but it will be great if this plugin can override the code. This happen because I use blogging client to publish my posts.

    • c.bavota on April 16, 2009 at 12:21 am

      It might be because you use a blogging client. I don’t have much experience with them so I really couldn’t tell you for sure. But I feel like blaming something else so I will say, yes, it is because of your blogging client. ;)

  23. raja on April 6, 2009 at 8:21 am

    Hi, thanks for the script… it shows first two images only 3rd one is not showing… can you pls check it.

    Thanks
    Raja.D

    • c.bavota on April 16, 2009 at 12:22 am

      I’ll take a look and try to figure out what is up.

  24. Frank on April 11, 2009 at 6:13 pm

    Dear Anyone,

    I am very new to Word Press (like one week or so) and apologize in advance for my probably very stupid question. Specifically, my template front page shows 4 different categories, and within each category, will be a 3 line text extract of a post, accompanied by a thumbnail sized icon image. The icon image is used for every single post in every category. Instead I want to show a thumbnail of the image I put into the post.

    Two questions please. Is that what this plug in does? Secondly, the instructions say somewhere to copy a line of code ‘into the loop’. I am embarrassed to say, that I do not know what this means. Could someone please help?

    Many thanks!

    Frank

    • c.bavota on April 16, 2009 at 12:24 am

      Hey Frank,

      If you take a look at your index.php file, you will see a part where you have a while (have_posts()) : the_post();. This is your loop. It might say “if” instead of “while” but it is still your loop. All that means is that the loop goes through and finds each post and makes it appear. If you place the code from this plugin within the loop, it will call the image from the post and display it. Put it before the the_content() function or the the_excerpt() function.

  25. brendan on April 16, 2009 at 5:44 pm

    hello.

    i’m having the same problem as Pete above. My images are getting distored really bad. how do i make sure that doesnt happen?