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’ />”;
}
?>

One Response to “Wordpress PHP Code Snip - Assign image to post ID”
Leave a Reply



