Support » Themes and Templates » linking images to posts

  • Resolved gegresh


    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.

Viewing 15 replies - 1 through 15 (of 30 total)
  • 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.

    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…

    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.

    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.

    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.

    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.


    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…

    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.

    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.

    “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.

    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.

    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.

    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.

    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

    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.

Viewing 15 replies - 1 through 15 (of 30 total)
  • The topic ‘linking images to posts’ is closed to new replies.