Background
Previously we covered, Displaying Author Pic and Bio in Your WordPress Post the Non-Gravatar Way a technique used to add an author block to the bottom of your posts in WordPress. Originally this technique was used to add an author block to articles written by an author of a specific category. By using Custom Category, Page and Post Templates for WordPress I was able to create a post template for a specific category knowing that one author would be contributing to that category. This works great for custom categories with custom post templates but what if you had a WordPress install with multiple authors, some who had bio and others who did not?
The Problem
The Displaying Author Pic and Bio in Your WordPress Post the Non-Gravatar Way technique made the author block appear on every single post in your WordPress install, let’s take the idea one step further and enable you to choose which posts to display a author block using the WordPress Custom Fields.
The Solution
We will be making edits to your single.php file, please make a backup of your originally single.php file just in case something goes wrong and you need to revert.
We will be using the author block example from Displaying Author Pic and Bio in Your WordPress Post the Non-Gravatar Way which is:
<div class="authbio">
<img src="<?php bloginfo('template_url'); ?>/images/<?php the_author_firstname(); ?>.jpg" alt="" class="alignleft"/>
<?php the_author_description(); ?>
</div>
We will also be using WordPress Custom Fields to trigger wether or not a WordPress Post will show an author block or not. For this example I added the author bio after the article, right before the share options and comments in single.php
<?php
if($authbio = get_post_meta($post->ID, "authbio", "true")) {
?>
<div class="authbio">
<img src="<?php bloginfo('template_url'); ?>/images/<?php the_author_firstname(); ?>.jpg" alt="" class="alignleft"/>
<?php the_author_description(); ?>
</div>
<?php } else {?>
*
<?php }?>
What the above means:
WordPress checks for a Custom Field Key called “authbio” to see if there is any value. If there is a value, display the authbio div which includes our author pic and a short bio. If there is no “authbio” Custom Field Key then display *.
Essentially you can write a post, if you wish to display the author box then add the field “authbio” and give it a value of yes if you do not wish to display any author box then don’t add any field to that post.
For example purposes, this post had a custom field of “authbio” with a value of yes, you can see the author bio and pic at the bottom of the post. Please take a look at every other post on this site…you will notice that after every post you will see a * as this is the only post on this site with an author box.
<class=”head”>Things to remember:
-If you are going to use this technique in your WordPress site remove the * or else you will have a * after every post with no author pic and bio, just leave it blank.
-You do not need to call your Custom Field Key “authbio” you can label it anything you want, just use the same naming convention in your php in the single.php.
-This posts references Displaying Author Pic and Bio in Your WordPress Post the Non-Gravatar Way several times, please read it if you have any questions about how to create an author bio and pic box.
Hope this helps, enjoy!
Leave a Reply