Objective:
To have the option to choose which signature image to insert as part of your blog post using custom fields in WordPress Blog Post.
Defination:
This is a sample code to implement different image signatures in your blog post based on a defined variable called "sig" from the custom field.
Instructions
- Step 1: Create a folder in your website root directory called "authors".
- Step 2: Upload the signature image files. (For this example we have 3 difference signatures)
- Step 3: Edit the code below to reflect the name of the image signature files
- Step 4: Insert the code in Main Index Template (index.php) such that it is after your blog post & before your tags or comments code.
e.g.
<--- Blog Post Content Code here ---> - Usually: <?php the_content(__('Read more', 'studiopress'));?>
<--- Copy code here --->
<---tag code --->
For this example, I am using the 1 or 2 to determined if i plan to use signature 1 or 2 respectively. If I do not define a number it takes the default signature "3" as the main signature.
<?php
//This part of the code inserts the value pulled from the custom field ("sig") in the blog post of WordPress to the variable "$signature"
$signature = get_post_meta($post->ID, 'sig', $single = true);
// This then does a check to see if signature 1 is needed
if ($signature == "1")
{
// Input your 1st signature file name
echo "<img src='/authors/avatar_1.png' />";
}
// This then does a check to see if signature 2 is needed
elseif ($signature == "2")
{
// Input your 2nd signature file name
echo "<img src='/authors/avatar_2.png' />";
}
// This then loads signature 3 as defauly as signature 1&2 condition was not fulfilled
else
{
// Input your 3rd signature file name
echo "<img src='/authors/avatar_3.png' />";
}
?>
Hope this helps everyone as it works great for me.
Andrew