December 2

WordPress: Guest Author Name and Bio on Template Without All Custom Fields

WordPress: Guest Author Name and Bio on Template Without All Custom Fields

My brother Barry, the inspiration for writing the Guest Author function spoken of in this post

Every once in awhile, I wanted to have people other than myself write some guest posts on this blog, like their own Top 5—I obviously don’t update enough! However, it was a nightmare trying to find a way to display the guest author’s name (as referenced by their user account in WordPress) and a short bio about who they are (to, ideally, further validate their reason for writing about a certain topic).

It sounds simple, and with the power of WordPress’s custom fields, it would be. But the trick was that I didn’t want it to display anything at all if it was posted by myself (since I’m the author of almost 99% of the posts). Displaying my name then would be redundant. I only wanted it to show if it were NOT me, and I wanted it to be easy.

You can see this technique in use on this blog on the Top 5 Worst Trades/Acquisitions in the 2009 Baseball Season post, authored by my brother Barry.

Here were the non-negotiables:

  1. The post author had to come from the Post Author option in WordPress—NOT from a custom field (e.g. the logged in user wouldn’t have to fill out anything to denote the post as written by them)
  2. The bio WOULD come from a custom field, to be used on a case-by-case (post-by-post) basis so that the author information/bio could be changed based on the post topic
  3. We WOULD NOT create a separate post template (e.g. post-guest-author.php) to do any of this—it had to come from within the single.php template

First Half: Display the author name (or not)

The Theory

  1. WordPress would go to a post page.
  2. It would then check a global variable to see if the author was one listed by user ID.
  3. If it was listed (e.g. blacklisted), don’t display anything. (This would prevent the blog from showing my name if the post was by me.)
  4. If it wasn’t the author listed, it would then print the Display Name of the author who did make the post.

The Code

Use this code within the Loop on your single.php template:

<?php
       global $authordata;
       if ($authordata->ID != 1) {
              echo '<p class="author-meta">By ' . $authordata->display_name . '</p>';
       }
?>

Notes:

  • The number ‘1′ in the above code references the user ID. By default, the ‘admin’ user name that comes standard with every WordPress install is user ID ‘1′. If you choose to log in using a separate user name, make sure you reference the correct user ID.
  • I’m choosing to wrap the author’s Display Name with the class “author-meta” so I can style it accordingly.
  • If you want to print something other than the Display Name (their nickname, first name, etc.) you can use any of the following values instead (or combinations of them):
$authordata->display_name;
$authordata->first_name;
$authordata->last_name;
$authordata->nickname;
$authordata->user_login;
$authordata->user_nicename;

Second Half: Add in bio information from a custom field

Instead of just having the name of the author, I also wanted to be able to customize the bio for each post. This would allow me (or the author) to tailor their bio to the content of that specific post. If it were baseball-related, they could provide notes about their experience with baseball. If it were *shudder* health-related, notes about their health credentials, etc.

To do this, you have to create a custom field called “authorinfo” on the post itself. For the value, type in whatever bio your heart desires. (For more information on adding a custom field, check out the first part of ForTheLose.org’s How To: Custom Fields post.)

The Code

Next, use this code within the Loop on your single.php template to display the custom field:

<?php if ( get_post_meta($post->ID, 'authorinfo', true) ) { ?>
	<p><em><?php echo get_post_meta($post->ID, 'authorinfo', $single = true); ?></em></p>
<?php } ?>

The whole thing

…but I wanted it wrapped up in one nice, overarching if statement. So here’s the whole code, all wrapped up into one nice php snippet:

<?php
	global $authordata;
	if ($authordata->ID != 1) { ?>
	<p class="author-meta">By <?php echo $authordata->display_name ?></p>
	<?php if ( get_post_meta($post->ID, 'authorinfo', true) ) { ?>
		<p><em><?php echo get_post_meta($post->ID, 'authorinfo', $single = true); ?></em></p>
	<?php } ?>
<?php } ?>

Drop that sucker inside your WP Loop and customize it like whoa.

Done with help from my buddy Glen. Make sure to check out his blog!

Leave a Reply