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

www.pingable.org

5 Responses to “Wordpress PHP Code Snip - Assign image to post ID”

  1. Quinn from PS3 for sale on August 16th, 2008 10:07 pm

    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?

  2. Amy from Free Xbox 360 on June 12th, 2009 11:03 am

    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?

  3. Kolorowanki on June 29th, 2009 10:19 am

    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

  4. Gify on July 3rd, 2009 4:16 pm

    “does is assign an image to each unique post ID” So it’s like a avatar? I will try to do something like a user could upload its avatars… Thanks for the tips.
    Regards,
    Janet

Trackbacks:

  1. » WordPress-Tips: Ein Bild je Post — cne _LOG Archiv

Leave a Reply




Comment Policy: This site uses KeywordLuv. Enter YourName@YourKeywords in the Name field to take advantage. Comments that use keywords in the name field in the incorrect format will not be approved. Links to inappropriate sites will also not be approved. Do not bother wasting my time by thanking me for a post. Add something to the discussion or your comment will be deleted