[resolved] linking images to posts (31 posts)

  1. gegresh
    Posted 10 years ago #

    Hi there. I'm working on a heavy metal magazine site using WordPress and I'm looking for a solution. Every article on the site has a corresponding image. I'd like to be able to easily associate those images with the posts they correspond to so that I can include the image automatically with the posts (and on the archive/index pages) i.e. I'm looking for some kind of function that would get the image that corresponds to an article. Some wonderful mystical function like "getImage()". It would be oh so nice if it were that simple.

    I understand that I might be able to accomplish something like this with custom fields, but it would be even nicer if I could use the built-in image uploading, since the site will have multiple contributors uploading their own graphics (album art, etc).

    Is this the kind of thing that would be easier with a plug-in? Thanks for the help.

  2. Kafkaesqui

    Posted 10 years ago #

    How about a plugin that provides a "getImage()" sort of function you can add to your templates and forget about it?

    I'm liking the idea. So much so I'm writing it now! I'll reply when I have it online.

  3. sgluskin
    Posted 10 years ago #

    I don't get how this is different than what is built-in to WordPress. You upload a pic and then put the url of it in your post. Whether that post appears on the blogs home page or in the archives, the image goes with it.

    I'm obviously missing something. Please share...

  4. Kafkaesqui

    Posted 10 years ago #

    In this case we're talking about automating the "then put the url of it in your post" part of it. By adding it through the template you can assign class/css properties by default. And there's little concern over whether the "uploader" is getting things right as to where the image should be placed in the post's content, etc.

  5. gegresh
    Posted 10 years ago #

    Incredible, Kafkaesqui! Thanks so much, I was sifting through pages and pages of forum posts for an answer and not finding anything.

    A few ideas about the plug-in:

    - It would be great if it was integrated with the built-in WordPress image uploader so that a call of, say getImage(theID()); automatically gave you the first image uploaded for the post.

    - It would be even better if you could somehow assign an offset and a number of images option to the function, so that if there is more than one image you could easily get just the third one, or the third through fourth, for example.

    After seeing this amazing show of support from WordPress users, I am definitely a user for life.

  6. gegresh
    Posted 10 years ago #

    Yeah sgluskin, ditto on basically everything Kafkaesqui said. To give you an example, I need it for the template I'm working on because I want to be able to put the image for the post in a place other than the main post area. I need the image to be in a separate table cell than the main content. I also want to be able to use it for category archive pages. Basically, I just need to separate it from the text content.

    I'm also concerned with having a number of contributors to my site (who aren't that computer-literate in the first place) spending too much time renaming and editing images to get them to show up in the template in the correct places.

    Something like getImage() would allow me to do what I want with images at the template stage, so I don't have to institute a workflow for images that my contributors need to worry about.

  7. sgluskin
    Posted 10 years ago #

    Yes, yes! My kids (elementary school age) are getting very handy with using our new "dogblog" -- but I do the photo uploading for now. I'm concerned that there are too many steps in the workflow to have them do it. And I'd love for them to be independant with this.

    Add me to the group of people waiting for Kafkaesqui's getImage() function.


  8. Kafkaesqui

    Posted 10 years ago #

    Plugin basics are done (and functional!). Here's some important bullet points on it as of now:

    * Function/template tag will be post_image() (seemed an obvious name).

    * Displays first image attached to the post, or alternatively an image using either the postname (i.e. slug) or ID of that post for its filename; this is configurable through a parameter. Now if anyone can convince me an offset (or the ability to use multiple images) is a better option here...

    * Uses either full image or thumbnail (again, configurable).

    * Another (configurable) option is to display the url raw, or in an <img> tag -- the default -- with class, title, alt, and (correct!) width/height attributes.

    A little more testing and code cleanup before release...

  9. gegresh
    Posted 10 years ago #

    I really can't believe that I asked for some advice 4 hours earlier and it's now a plug-in.

    I think the only other thing I could ask for is some way to avoid the bad image link icon (or possible php error) that would result from me calling post_image() for a post that didn't have an image uploaded to it. A number of the bands we're going to be reviewing for my site are doing self-releases and don't even have cover art. If there was some way for the plug-in to automatically just fill the space the photo would take if there's not an image available would be perfect.

  10. Kafkaesqui

    Posted 10 years ago #

    "avoid the bad image link icon (or possible php error)"

    The plugin should already fail silently for no image attachment. I'll slip in some file checking to make sure it's actually there, and drop in another parameter for a default image to display if none exists.

  11. gegresh
    Posted 10 years ago #

    "I'll slip in some file checking to make sure it's actually there, and drop in another parameter for a default image to display if none exists."

    Beautiful, thanks a lot.

  12. Kafkaesqui

    Posted 10 years ago #

    Post Image plugin is online:


    Only change from functionality mentioned above is, I pulled the parameter configuring whether to use the first image attachment, or look for one with id/postname as filename. Instead it uses the first UNLESS an attachment with id/postname exists. Now, time for a nap.

  13. gegresh
    Posted 10 years ago #

    You earned it, my friend. Now while you're napping, I'm gonna go kick the tires of this thing. Thank you muchly.

    I'll post back here with any comments. And probably gushing praise.

  14. gegresh
    Posted 10 years ago #

    Everything works as expected. Thanks so much for giving me a solution that was so simple and flexible. If you want to see how it worked out for me, you can check out the site:


    If you click around, you'll see that some articles use a default image (a "B&P" logo) like you talked about. The site's not opening til March 10th, so it's still on my testing server and still undergoing plenty of design and content changes. But with this plugin the rest of the development should go a lot smoother than anticipated. We'll be thanking and linking you on our credits page. Thanks again.

  15. antonskey
    Posted 10 years ago #

    a solution we came up with at our site is to simply upload an image to a specific folder and use the post ID to name the image. for example, post42.jpg. You just have to know the post's ID obviously. then just call it as <img src="/folder/post<?php the_ID(); ?>.jpg" width="100px" height="100px" alt="supplemental article photo" />

    Not sure if that helps or not, but it's one work-around.

    You can see it in action at canadanoise.com

  16. gegresh
    Posted 10 years ago #

    Yeah, antonskey, that's actually what we were doing before getting our hands on kafkaesqui's excellent post_image plugin. There were a couple problems with the method you talked about, though. First of all, some of the album reviews we needed to fetch images for were from demo albums that didn't have artwork. We wanted a way to automatically call a default image when an image wasn't available (which post_image provides). I was also concerned with complicating the workflow for adding images when a lot of the contributors to our site aren't that computer-savy. Being able to just upload an image with any filename directly through the post page in WordPress and have it automatically associate itself with the post made things work a lot smoother. I highly recommend post_image.

  17. Welby
    Posted 10 years ago #

    How does this differ from the Post Icons plug-in?

  18. Kafkaesqui

    Posted 10 years ago #

    Welby, I looked at that plugin (as a possible solution for gegresh) before cooking up mine. I'd say the main 'differ' here, beyond the scope of being WP2-specific, is the further automation in Post Image. Just upload your image and you're done.

  19. tvdwalt
    Posted 10 years ago #

    Hi Kafkaesqui
    Your plug-in is exactly what I have been hunting for, but there is one snag. I'm running WP 1.5.3. I am going to be upgrading soon, but would it be possible for you to make this plug-in work under my version?
    My site goes live on Friday 8-|


  20. tvdwalt
    Posted 10 years ago #

    I bit the bullent and upgraded. Thanx anyway.

  21. Kafkaesqui

    Posted 10 years ago #

    tvdwalt, I'm glad you stepped up to 2.0. Hope you like the plugin.

    For those still at 1.5.x and who cannot make the move as tvdwalt did, Welby's suggestion above is a good one (I can't downgrade my plugin without cutting out the main reason I wrote it, which is to hook into the image attachment features of WordPress 2).

  22. Ryan Fitzer
    Posted 10 years ago #

    Tried using this in a double loop and for some reason it will only pull the thumbnails for the first loop but not the second. Otherwise it works excellently. Any ideas? Here's the code I'm using for the second loop:

    <?php $my_query = new WP_Query('cat=7&showposts=2'); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

    <div class="post" id="post-<?php the_ID(); ?>">

    <div class="entry">

    <div class="postthumb"><a href="<?php the_permalink(); ?>"><img src="<?php post_image('', true, false); ?>" /></a></div>

    <div class="posttext">
    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
    <h4><?php the_excerpt(); ?></h4>

    <div class="clear"></div>



    <?php endwhile; ?>

  23. Gianko
    Posted 10 years ago #

    This is so great!

    Do you have a plugin that can help me in something like this http://wordpress.org/support/topic/74472?replies=1 ?

  24. madonnafl
    Posted 10 years ago #

    This has been the most helpful plugin out of the 10 I'm using on a website I'm currently creating (it saved me from writing it!). Really Kaf, awesome job. I know the thread is old but I had to comment on it. I'm also using your post template-by-category, get-a-post and add-link-attribute plugins. I'm going to test out your download manager plugin sometime today.

    It wasn't until this past week I learned how to read/understand the command structures and what not of PHP. I saw how you called the image via "get_post_custom" and used it to create custom meta fields for other media types and information as well! ^_^

  25. madonnafl
    Posted 10 years ago #

    Hi Kaf -- After looking at your post-image plug-in more and figuring out how to get the most out of it, there is absolutely no reason for the following in your code:

    40 if(!in_the_loop())
    41 return;

    It may be more appropriate to check if $post has a valid value, as it is the only real dependency in the function.

    Just my two cents! Thanks for the download manager plug-in -- it's working super!

  26. Marcomail
    Posted 10 years ago #

    This plugin works only with the original image uploader or also with IImageBrowser ?

  27. BPartch
    Posted 10 years ago #


    First let me say this plug in is awesome!

    Even though I have gotten it working I am still somewhat a novice with it as well as actually using the WP CP to upload the images.

    Until now I have just been FTP(ing) them to the server and linking to them manually in each post.

    So this plug in offers me a great solution for this.

    Though I am curious.

    I currently have the images in the posts linked to external sites and was wondering if there is a way to make this a possibility somehow with this plug in?

    Also if this is possible, is there a way to add rel="nofollow" to the link for the image?

    I realize this may not easy to accomplish as this would also require adding a url in the WP CP when the image is uploaded and somehow relating it to that specific image as well.

    Just curious.

    Thanks in advance for any assistance.

  28. sol123
    Posted 9 years ago #

    First of all, I would like to thank the writer of post-image plug in for the time he spent on this script and sharing with us. I was not able to use it so far, But I am sure many do. I was hoping that the writer or anyone with a knowledge of it can assist me and answer a few questions that I have as I only have basic knowledge of scripting.

    Our website is a sporting website and the url of front page is:

    As you can see, we have word press showing up in the body of our front page, including the images. We use FCKeditor to post images.
    What I want to do is to make the images appear as thumbnails in the latest news section with full image appearing when one clicks on a title to read the whole news. Something like the following:

    Please notice that I want the images to appear where there is an X as thumbnails of around 120*100 pixels. (Please disregard the text malalignment.)

    Now, for our current front page we use the following command to call the post content and the images:
    $textfive = substr((strip_tags($rowfive['post_content'],'<img>,')),0,380) ;
    echo " ".$textfive."... <span class=\"smallfont\">more ...</span>" ;

    This is a strip tag. the problem is, I cant separate the images from the text. they go together.

    Now to my question. Does post-image do what I aimed to do as explained above?

    If yes, I have went through the instructions. Uploaded the post image php under appropriate place and it shows up fine under plug ins and I can activate it. My problem starts there. There is a talk about "The Loop". I read the article about it, but still I have no clue where I can find it. When that issue is solved, where and what do I put the command that makes thumbnails of those images corresponding to each post?

    I had a technical person who did stuff like that for me. I learned quite abit of scripting in the past few months thanks to people like you. But I can neither get hold of my technical person, nor my knowledge has become sufficient enough to answer the above questions.

    I would really really appreciate any help that I can get in this matter.Thank you so very much.

    We are using wordpress version 2 and the template we use is connections 1.0. I dont know if with our setup, template matters or not at all.

  29. Ryan Fitzer
    Posted 9 years ago #

    Here's how I've have incorporated the post-image plugin in my index page. http://wordpress.pastebin.ca/185105

  30. Phil Johnston
    Posted 9 years ago #

    Im trying to take the same output of post-image and apply it to an RSS tag.
    However, <?php post_image(); ?> does not seem to work with RSS.
    Is there another way to call it from wp-rss2.php?

Topic Closed

This topic has been closed to new replies.

About this Topic