Use The Attachment Image!

Use the Attachment Image! is a WordPress plugin which allows the first attachment image associated with a post to be used in different locations throughout a blog, a perfect compliment for magazine themes.

This plugin can include images of nearly any size, simply by using the included < ?php use_the_attachment_image(); ?> function.

Download

This plugin is available for download here, from the WordPress plugin gallery.

Installation

To utilize this plugin, you will first need to upload use_attachment_image.php to the /wp-content/plugins/ directory on your web server; and activate the plugin through the ‘Plugins’ menu in WordPress.

Next, place < ?php use_the_attachment_image() ?> (will include a link to the post), or < ?php use_the_attachment_image_nolink() ?> (will NOT include a link to the post) in your template to include a thumbnail of the first attachment image. Note the function must be inside of a WordPress loop.

Function options

By default, this function will include a thumbnail image. To include images of different sizes, use the respective functions below. You may also use <?php use_the_attachment_image_nolink() ?> in place of  <?php use_the_attachment_image() ?> here.

Thumbnail < ?php use_the_attachment_image('thumbnail'); ?>
Medium < ?php use_the_attachment_image('medium'); ?>
Large < ?php use_the_attachment_image('large'); ?>
Full < ?php use_the_attachment_image('full'); ?>
20 px < ?php use_the_attachment_image(array(20,20)); ?>
Feel free to replace 20px with another specific size to best meet your needs.

This plugin also includes the capability to include a thumbnail image from each post to your RSS feed. To activate this feature, simply add this code to your functions.php file.

< ?php add_filter('the_excerpt_rss', 'insertAttachmentRSS'); ?>
< ?php add_filter('the_content_feed', 'insertAttachmentRSS'); ?>

More

See an example of how this plugin can be used.

If you have any questions about this plugin, feel free to ask below, and I will respond as my time allows.

Thanks for your interest!

54 Responses to Use The Attachment Image!

  1. Matías says:

    Hi!
    I’ve installed this plugin on my site, but when using it on any part of the loop, page stop loading where the image should be printed
    any ideas?
    thanks in advance

  2. Tom says:

    Thanks for the note.

    Could you try adding:

    define(‘WP_DEBUG’, true);

    to the top of your wp-config.php file? This will tell you if there are any php errors coming up. Let me know if this produces any errors. If it does not, I would recommend you remove this line when you are done diagnosing.

    I will take a look at the other plugin you mentioned, and see if I can see anything.

    Thanks for helping improve this plugin!

    Tom

    • Tom says:

      I am not seeing anything right off hand. Let me know what turning on wp_debug uncovers, because I would like to get this fixed if there are any problems.

      Thanks!

      Tom

    • Matías says:

      I’m getting

      Fatal error: Call to undefined function special_get_post_photo() in /var/www/taringacs.net/public_html/wp-content/themes/taringa/index.php on line 32

      Plugin it’s installed!

      • Tom says:

        Oh, It looks like I made a mistake in the documentation!

        You want to use use_the_attachment_image() instead of special_get_post_photo()!

        I will make an update to the plugin with the correct variables in the documentation in the next few minutes. Sorry for the problems; thanks for helping diagnose them. Let me know if this doe not fix your problems when you download the new version.

  3. Tom says:

    Okay, the new version (1.01) should be available for download now. Grab a copy, and let me know if you still have problems.

    Tom

    • Matías says:

      Great! Now it’s working fine. Thanks for your work!
      I’ve have a request to made, don’t you wanna try to make this plugin show the first image of the post resized using html if it hasn’t got any attachments?

      Sometimes images are linked but no attached, and your plugins shows nothing actually!

      Thanks!

      • Tom says:

        Thanks for the note. I am glad to hear that things are working again!

        That is a good idea, I will consider it for future releases.

      • Matías says:

        Thanks Tom
        BTW, is it possible to check using your plugin if the post got any attachment?
        like if (use_the_attachment_image(‘thumbnail’) != ”)

      • Tom says:

        It is probably possible to do so. I have not tried it. I would, however use the function wp_get_attachment_image from core for this, so you are not plugin dependent for this function. Search Google for this function, and it should point you in the right direction.

        Let me know if this is confusing.

  4. Afark says:

    Tom,

    I’m trying to get your plugin to add an image from each post to the rss feed. I activated the plugin & added:


    to the functions.php file in my theme. I then resynced the feed at Feedburner and cleared my browser’s cache, but I don’t see any images on the feed. FYI, I’m using the FD Feedburner plugin. Also, the images in my posts were all uploaded using the WP gallery feature & shortcode as opposed to being “inserted”.

    I’d appreciate your help.

  5. Afark says:

    Oops. It looks like it’s working now. Feel free to disregard my earlier question. Thanks for the plugin, it solved my problem of trying to add an image to the rss feed.

    • Tom says:

      I think that in many cases you need to add a new post before the feed will refresh itself. Perhaps this solved the problem for you. Glad to hear things are working.

      Tom

  6. Matías says:

    Tom, i can’t make a conditional to check if the post got any attachment, any ideas? i’m trying with wp_get_attachment_image(’1′) without success

    • Tom says:

      I think something like this would probably work:

      if (wp_get_attachment_image{} == “”) {} else {}

      Put what you want it to do if the image is not present in the else statement.

      Tom

      • Matías says:

        Tom, this if (wp_get_attachment_image() == “”) { it’s always true… i can’t get any negative case and i’m sure post got attachment images.

        I’m trying to use something like this

        if (wp_get_attachment_image() == “”) {
        images(’1′, ’200′, ”, ”, true);
        }else {
        use_the_attachment_image(‘full’);
        }

        To use your plugin, and when posts doesn’t have attachments, another plugin pull the first image and it’s resized

        Thanks for your help

      • Tom says:

        Thanks for the note. I have been super busy… I will look at this soon, though, and let you know if I have ideas.

        Thanks.

        Tom

  7. Mobi says:

    Good plugin :)
    thanks

  8. Pedro Costa says:

    Hi Tom ..
    I do not understand where to connect the picture to post…

    Thanks for your help

    • Tom says:

      Actually, you don’t have to specify the image yourself. It automatically selects the first image attached to the post. If you upload images using the image uploader, it automatically picks the one with the highest rank order.

      Let me know if you have any other questions.

      Tom

  9. Jake Song says:

    Dear Tom.
    I think It is very useful to me, so I will use it.
    But I have a question.
    It have many options for the thumbnail size like ‘array(20,20)’.
    I want to decrease 50% for the original image.
    What option I can use.
    Please help.

    • Tom says:

      Jake,

      Thanks for the comment. Glad to hear that this is useful to you.

      The function that allows you to make an image a specific sizes does not allow for the use of percentages. I would look at the size of the image you are trying to include, cut the dimensions in half, and use those values in an array.

      For instance if the original image is 200px x 200px, you make the image half of that size, 100px x 100px by using the function: ‘array(20,20)’.

      Happy Easter!

  10. Vero says:

    Hi
    I just wan’t to THAAAAAAAANK you for this plugin!
    It’s really saved my time and gived to me a lot of options on my blog so, TAHNK YOU for this great job

  11. Lanre o. e. says:

    “Thumbnail
    Medium
    Large
    Full
    20 px ”

    On which file do i place this functions?

  12. Lanre o. e. says:

    On which page do i paste the Function options codes?

  13. Tom says:

    Hi Lanre,

    Thanks for the comment! You want to place the function in the PHP template file relating to the place where you want the image to show up.

    Let me know if that does not make sense.

    Thanks!

    Tom

  14. Tom says:

    Thanks for the comment, Lanre. Instead of putting the code in each post, you want to put the code in the template file. By adding the code to your template file, the image will be applied to each post automatically on the homepage, with no action necessary on any of the posts.

    You have a WordPress “theme” installed on your server. The theme controls the design of your site. You want to insert the code in the spot you want the thumbnail inside of your template file. To modify the homepage, you will likely want to look in /wp-content/plugins/PLUGIN_NAME/index.php /wp-content or /plugins/PLUGIN_NAME/home.php (could vary based on your theme setup), and add the code in the correct spot there. The code must be inserted within a WordPress loop to work.

    Let me know if you have any questions.

  15. Bruce says:

    I am using this plug in for my rss feed, it works great! thank you very much for this piece of code!

  16. Vitalik says:

    Great plug-in. I use it for my site http://azovsea.in.ua/ . I think i found some bugs. Your plug-in doesn`t display thumbnail for the post if you try to get it from the already used thumbnails (for other posts). So i have to load a new picture to get the correct results.
    Also it will be great if your plug-in can be used with NextGen Gallery for WP. Thanks.

    • Tom says:

      Thanks for the note. I am glad the plugin is helpful.

      Unfortunately, images must be attached to a post for them to be able to be used with this plugin. If you uploaded an image with a different post, you are correct, it will not be included even if it was used in another post.

      For my purposes this has worked well, as images do not have to be posted in a post, they simply have to be attached to the post. I am not sure how one would best go about pulling images that are included with the way WordPress code works, but not attached to a page. I suppose one could look for all of the image tags in a post somehow. As you said, the work around is to re-upload images.

      This plugin can probably be used with NextGen Gallery, but would require some integration with NextGen. That isn’t something that I will likely work on in the near future, but it is a good idea.

      Thanks again for using this plugin!

  17. Dan Baker says:

    Hi,
    I am using Wp as a CMS and this plug-in does exactly what i need …except… what i would like to do is simply link the ‘thumbnail’ to the larger image so i can use Lightbox. Is this possible?

    I am feeding the content to an external site – so a link to the ‘hidden’ cms blog is a no-no.

    Any help is appreciated.
    many thanks!

    • Tom says:

      Hi Dan,

      Thanks for the note. I am working on another website with which I would like to use Lightbox also. It is probably possible, but looks a bit difficult. I will post back here if I come up with a solution.

  18. JayNL says:

    hi, thank you this is a nice plugin. Thanks for creating it.

    Would it be possible to add a specific class to the image?

    something like
    `
    use_the_attachment_image_nolink(‘size=full&css_class=imageclassname’);
    `

    • Tom says:

      Hi Jay,

      Thanks for the comment. This plugin makes use of the WordPress function `wp_get_attachment_image`, which includes the whole image tag. The plugin just tells it which image to grab. Unfortunately, you would have to modify this aspect of the plugin in order to do this. You could write out all of the code for including the image, and include the image with `wp_get_attachment_image_src` instead. It might also be possible to filter `wp_get_attachment_image` to include a CSS class.

      Depending on what you are trying to to, it may be easier to just include a div tag around the image.

      Let me know if you have any other questions.

  19. Tom says:

    A couple of people have mentioned that they would be interested in providing some sort of alternative content if there is not an image to be shown. The best way that I can think of to do this would be to place this content (or set a variable equal to something) within the else at the end of each function.

    Let me know if you have any questions about this or anything else.

  20. Tom says:

    Version 1.02 of this plugin has been released. See the download page at the WordPress plugin repository.

  21. Amity says:

    Hi Tom and thanks for your plugin. First of all, i’m french so i’m sorry for my language. I have a problem with timthumb, a problem of cache memory (i’m not sure it’s the good term).

    Anyway, i seek a alternate soluce, and i find your plugin. I have test your plugin in my local server, i use in loop … but the image have for size 50px / 50px … i don’t know why :/

    Other problem, this plugin take the first image of my post, but when i change my image, the thumbnail shows the same image .. :(

    If you can respond it’s very nice !
    Thanks a lot
    Amity.

  22. Amity says:

    Oh i see that my post is cut. When i try to use your function array (95,50) cause i want my image has for size 95px / 50px but my image is 50/50.

    Re-Thanks a lot
    Amity

    • Tom says:

      Hi Amity, Thanks for the note. Glad to hear you are using my plugin.

      Check your function with the documentation one more time — It’s important that the parenthesis are in the right places. The function should look something like this: `use_the_attachment_image(array(95,50));`

      Also, it’s important to note that my plugin simply shows an image as a different size, but it does not actually make a smaller copy of the image on the server in the way Timthumb does. For this reason, it may make sense to talk with your host and try to get the memory issues resolved if Timthumb was working well with your site. Alternatively, SLIR works similarly to Timthumb. You might want to check it out if you can’t get Timthumb to work and need its functionality.

      Let me know if you have any additional questions.

  23. max says:

    Ciao , i’m trayng to use the plugin in feed but no image show!
    looking in the source code of the page i found instead o the image :
    i put the code in function.php All my post have 3 or 4 attach post image.
    thanks

    • Tom Lany says:

      Hi Max,

      Sorry this plugin isn’t working for you. Have you tried simply activating the plugin on your site, instead of placing the code in functions.php? Also, make sure that you are calling this function within a WordPress loop.

  24. Derek Hughes says:

    I’m sorry, I cant seem to get this to work. I have an image attached to a post entitled Product 3, and when I insert inbetween my div to hold an image, I get a blank.

    Here’s my code by the way:

    <div class="post" id="post-”>

    Your help is greatly appreciated :)

    Derek

  25. Hello,
    I am trying to have Thumbnail image together in my post. If you check my web – for example RECENT POSTS – text from RSS if fine but Thumbnail image must be always added manually – even if is attached in RSS. Could you help me with this – how to added Thumbnail image to my post?

    Thank you and Have a nice day from Czech Republic

    • Tom Lany says:

      Hi Petr,

      Thanks for the note. It’s great to see you’re using this plugin. In order to include an image in the RSS feed automatically, you need to add some code to your theme’s functions.php file. See the bottom of this installation instruction page for more information. It looks like you currently have at least two images being displayed for each item in your RSS feed. I’m not sure if these were added by this or another script, or if they were added manually. Let me know if you have any other questions or need more help.

      Thanks, and have a great day!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>