WordPress PHP Code Snip – Assign image to post ID
The following PHP code is some code I use on this blog that I thought some others that don’t program may find useful. What it does is assign an image to each unique post ID from WordPress that is a gif file. If you put a file in the image folder in your root directory of your Website called 8.gif – the 8th post you made on your blog will be assigned that image wherever you place this code. If there is no image for that id (you haven’t put a file called 8.gif in yet) it assigns the image called uplogo.gif. Demo. Of course you can change any of the directories, file extensions, and the alternative image name to suit your own needs.You can find the post ID by looking in manage posts from your dash. You should use this code in template files.
<?php
$image = "$id.gif";
if(is_readable("./images/$image"))
{
echo "<img src='/images/$image' alt='alt tag' />";
}
else
{
echo "<img src='/images/uplogo.gif' alt='alt tag' />";
}
?>
Check out some other WordPress hacks: WordPress hacks

Trackback from your site.





Comments (16)
I have to admit I’ve never seen this code before…this php is simply on a per session basis or stored in the database?
Hmm, I was wondering that Quinn. I obviously don’t know enough about php. Is there no other way of doing this? Or is this the easiest way?
Great tutorial, I only draw but will pass your post address to my programmer. He will know how to use it. Thanks for sharing.
Regards,
Matt Kolorowanki,
Illustrator
One issue i am having is only the uplogo.gif shows. I have uploaded 1.gif, 2,gif etc but now sure how the code references those images for posts 1 and 2 etc. I am a designer really not a developer otherwise this would be no problem. thanks.
Hi Matt, to find the ID of each post you go to your edit post dashboard, and hold your mouse over the post title, you will see the page it wants to link to has an ID on the end of it. You use this ID for the number you give your image file. Or if you edit any post, the ID is the number at the end of the URL, when editing! Hope that helps.
great code, helped me figure out something on my custom template
Hmm, I was questioning that Quinn. I clearly don’t know ample about php. Is there no other way of undertaking this? Or is this the least complicated way
The aesthetics of this design are beautiful, very contemporary. I was looking for custom designs, I’m just trying to craft the nicest use for this one. I’ll have to get my designer to incorporate this for me. What does the back end of this look like, do the pictures display next to each post, or is it fiddly in that respect? Thanks
Thanks for the your wordpress PHP code for using this ID for the number you give your each image file. Very helpful
hi
i tested the code and it failed!
I think an errore occured in the definition of
$image = “$id.gif”
I check it out by using printf() function after placing it in the loop. the printf() function returns the file name and suffix without any dot!(imean “.”) I dont know why but it happens.
please help!
Thank you for sharing this little PHP code. I was looking for something like this with my images and I am glad I found it. I’m looking forward to reading a lot more of your site in the future.
Thanks for all of the great information and links. There are so many people who complicate things, but this code is great. Besides all of the information you have provided, the links are also very helpful and informative. Thanks again!
Thanks a lot for your share. Do you know an plugin or script to forbid the copy of image ?
Hi Simon,
Funny you have the same name as me. Anyway just wanted to say thanks for this PHP code it really came in handy.
Simon Keller
Using the id fo the post is clever for people who don’t know much of php. I do use extra custom fields to do such things myself