Post Pic

Rewrite author name with custom field

In my other blog Cats Who Blog, I have demonstrated how being a guest blogger is definitely a great way to gain new readers. In order to give your guest authors some credits, you should definitely implement this easy tip.

Nothing hard with this recipe: Simply paste the following code on your single.php and page.php, where you want the author name to be displayed.

<?php $author = get_post_meta($post->ID, "guest-author", true);
if ($author != "") {
    echo $author;
} else {
    the_author();
}  ?>

Once done, you just have to create a custom field named guest-author and type your custom title as a value.

9 Responses

Nov 09 2009 11:19

I personally prefer to create accounts for my guest authors… But there are sure cases where this is better.

Nov 09 2009 18:17

I like creating the guest accounts as well because it allows me to more easily display their gravatar and their author information at the bottom of each post.

Nov 12 2009 04:16

?> to close the PHP line is missing

Nov 12 2009 05:03

This will be really helpful – thanks! I use a lot of guest author posts and I create accounts for those who guest author more than one or two posts but some people are once-offs and this is a great solution!

Nov 12 2009 08:23

@Ricky Buchanan : Fixed it, thanks!

Dec 23 2009 17:02

A more appropriate way to do this would be to drop this code in your theme’s functions.php file. This makes sure you cover all instances of the author name in all templates. This also makes it easier to carry changes from theme to theme and work along with child themes.

add_filter( 'the_author', 'custom_author_name' );
add_filter( 'get_the_author_display_name', 'custom_author_name' );

function custom_author_name( $name ) {
global $post;

$author = get_post_meta( $post->ID, 'guest-author', true );

if ( $author )
$name = $author;

return $name;
}

Dec 23 2009 17:18

@Justin Tadlock: Used the second filter hook for something similar, thanks for the suggestion! (Some filter hooks really are a secret! They should extend their documentation, really) (In case you want to know what for I used it, it’s a plugin ‘WT Co-authors’ in the WP plugin directory, just saying)

Feb 18 2010 02:51

Thanks a million!
We are using wordpress as a CMS for a student newspaper. I utilized this to avoid having to create a user account for every freelancer, and still give them a byline.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required