Multiple Authors for a Post

This article was originally published in January 2011.
I rewrote the article in November 2012 to take advantage of new features developed since then.
David Gray wrote a follow-up article in September 2014 which you might also want to review.

If you have a magazine-like website, you might need to specify multiple authors for a single post. There’s a great plugin called Co-Authors Plus that will let you do this.

Once installed, you can use many template tags, or you can use the get_coauthors() function to retrieve all the author data and use it however you like.

Authors Shortcode

The Post Info area of Genesis can be customized using a filter. We could drop the coauthors_posts_links() function in there to list authors and link to their archive pages, but it would be even better to create a shortcode to be used in the post info section. If that function isn’t available (ex: someone disabled the plugin), it will use the standard get_author_posts_url() instead.

We will create a shortcode called [post_authors_post_link] with the following parameters:

  • between – what to display between authors, default is a comma
  • between_last – what to display between the last two authors, default is “and”
  • before – what to display before the list of authors, default is empty
  • after – what to display after the list of authors, default is empty

To set up the shortcode, add the following to your functions.php file or core functionality plugin:

/**
* Post Authors Post Link Shortcode
*
* @author Bill Erickson
* @link http://www.billerickson.net/wordpress-post-multiple-authors/
*
* @param array $atts
* @return string $authors
*/
function be_post_authors_post_link_shortcode( $atts ) {
$atts = shortcode_atts( array(
'between' => null,
'between_last' => null,
'before' => null,
'after' => null
), $atts );
$authors = function_exists( 'coauthors_posts_links' ) ? coauthors_posts_links( $atts['between'], $atts['between_last'], $atts['before'], $atts['after'], false ) : $atts['before'] . get_author_posts_url() . $atts['after'];
return $authors;
}
add_shortcode( 'post_authors_post_link', 'be_post_authors_post_link_shortcode' );
view raw shortcodes.php hosted with ❤ by GitHub

Now if we want to update the Genesis Post Info to display all post authors (with “by” right before the listing), we’d add this to functions.php:

/**
* List Authors in Genesis Post Info
*
* @author Bill Erickson
* @link http://www.billerickson.net/wordpress-post-multiple-authors/
*
* @param string $info
* @return string $info
*/
function be_post_info( $info ) {
$info = '[post_authors_post_link before="by "]';
return $info;
}
add_filter( 'genesis_post_info', 'be_post_info' );
view raw functions.php hosted with ❤ by GitHub

Author Boxes

Genesis also has a feature called the Author Box that displays the author information at the bottom of the post. But this doesn’t use Co-Authors Plus, so if you have multiple authors only one will show up at the bottom.

First we’ll remove the Genesis Author Box. Then we’ll copy the code into our own function, but this one will accept an author ID field so we can render any author with it. Finally, we’ll create a function that checks to see if Co-Authors Plus is in use, and if it is, display author boxes for all authors; if it isn’t, display the author box for the normal post author.

// Remove Genesis Author Box and load our own
remove_action( 'genesis_after_post', 'genesis_do_author_box_single' );
add_action( 'genesis_after_post', 'be_author_box' );
/**
* Load Author Boxes
*
* @author Bill Erickson
* @link http://www.billerickson.net/wordpress-post-multiple-authors/
*/
function be_author_box() {
if( !is_single() )
return;
if( function_exists( 'get_coauthors' ) ) {
$authors = get_coauthors();
foreach( $authors as $author )
be_do_author_box( $author->data->ID );
} else {
be_do_author_box( get_the_author_ID() );
}
}
/**
* Display Author Box
* Modified from Genesis to use an ID
*
* @author Bill Erickson
* @link http://www.billerickson.net/wordpress-post-multiple-authors/
*/
function be_do_author_box( $id = false ) {
if( ! $id )
return;
$authordata = get_userdata( $id );
$gravatar_size = apply_filters( 'genesis_author_box_gravatar_size', 70, $context );
$gravatar = get_avatar( get_the_author_meta( 'email', $id ), $gravatar_size );
$title = apply_filters( 'genesis_author_box_title', sprintf( '<strong>%s %s</strong>', __( 'About', 'genesis' ), get_the_author_meta( 'display_name', $id ) ), $context );
$description = wpautop( get_the_author_meta( 'description', $id ) );
/** The author box markup, contextual */
$pattern = $context == 'single' ? '<div class="author-box"><div>%s %s<br />%s</div></div><!-- end .authorbox-->' : '<div class="author-box">%s<h1>%s</h1><div>%s</div></div><!-- end .authorbox-->';
echo apply_filters( 'genesis_author_box', sprintf( $pattern, $gravatar, $title, $description ), $context, $pattern, $gravatar, $title, $description );
}
view raw single.php hosted with ❤ by GitHub

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments are closed. Continue the conversation with me on Twitter: @billerickson

Comments

  1. guiroo.com says

    Is this still working with an upgrade to Genesis 2.0? I have multiple authors in the post info but having trouble with the Author Box. Do I need to add the function to add the box before removing it? Not worry about that? I either have 1 box or none. Any help is appreciated. Thx!

  2. Lasse says

    Hi Bill,
    Thanks for all of your valuable wordpress tips!
    I am trying to implement your bit of code to enable co-authoring on my site. But whenever I try to alter the between_last value to something in my own language, I get the white screen of death. The reason might be that I am doing it wrong, so would you explaining in detail how to modify your bit of code?
    Many thanks,
    Lasse

  3. David Gray says

    Hi Bill,

    Great tutorial! Finally got it to work. I must have had a typo the last time I tried (last month). This may be out of scope of your post, but I was wondering how I could get the author links in the byline to link to their website instead of their archives. I thought I could just replace:

    get_author_posts_url() with the_author_link()

    in the first bit of code, that it would work … it didn’t. Are you able to offer a solution?

    Thanks!

    -David G.

    • Bill Erickson says

      the_author_link() echo’s the link. You’ll want to use get_the_author_link() since it returns it.

      • David Gray says

        Thanks for your reply! That’s actually what I meant … however, it does not work. Perhaps it’s something to do with the plugin — maybe the get_the_author_link() function does not exist in the Co-Authors plugin? I don’t know, and again, this is probably out of scope, but I’ve made more edits to the code below and it somewhat works — I just no longer get 2 authors in the byline … just one.

        https://gist.github.com/billerickson/a14c52ef4a11f8b2926f

          • David Gray says

            No problem. I checked out the Co-Authors Plus template tags again and it looks like I figured it out. It’s possible it’s not 100% perfect, but it is working. I’ll post the code later.

            Thanks again, Bill!

  4. Kim says

    I apologize for asking more clarification, but I’m a fairly new with WordPress. I’m a little confused as to which file I should be adding the code to show co-author boxes. Some post say the function.php and others show the single.php file. In my buddypress/bp-themes/ folder there is a single.php file that has code for the author-box (this is the only file I can see where author-box is referenced). So do I replace the code in single.php or functions.php?

    Thanks from a newbie.

    • Bill Erickson says

      I recommend putting it in your actual WordPress theme (in wp-content/themes/[your theme]), not in BuddyPress.

      Place it in single.php if you only want it appearing on single posts. Put it in functions.php if you want it available sitewide.

  5. Tahir says

    Can you please tell me how can I display Co-Author image and bio. I am using twent-sixteen child theme.
    coauthors_posts_links(); display co author name with link to author’s posts. I don’t which template tag will output co-author image and bio.

  6. Patricia Barden says

    Thank you for the great tutorial.
    The code works great for displaying multiple “regular” users in an author box when using the Co-Authors Plus plugin.
    However, I can’t figure out how to get guest users to display in the author box.
    I’ve tried multiple versions of the following to no avail:

    foreach( $authors as $author ){
    if( isset( $author->data->ID ) ){
    be_do_author_box( $author->data->ID );
    } elseif( function_exists( ‘get_guest_author_by’ ) ) {
    $user = get_guest_author_by( ‘ID’, $author->id );
    be_do_author_box( $user->ID );
    }
    }
    Any help you can provide is greatly appreciated!

  7. Denise says

    Thanks so much for this code. I’m using the StudioPress Magazine Pro theme (albeit renamed). The code to display the second author’s name worked perfectly for me, but the second author box isn’t appearing. I tried the code for HTML5 themes posted at diffchecker (thank you) but nothing changed. I know this post is a few years old; has there been a further update to the code? Thanks again.

    Here’s an example page: http://www.emptymirrorbooks.com/features/literature/interview-with-argentina-poet-juan-arabia.html

    • Bill Erickson says

      I’m seeing the second author box on that page. Were you able to fix it? Can you share what code changes were required?

  8. Kafi Khan says

    Hi
    I want to display multiple author using Avada Theme. Can I use this combination to work there ?

  9. Kip says

    Thank you so much for this. I had the co authors plug plugin installed, but couldn’t work out how to make it work with Genesis, but your tutorial (I am not a techie) helped me figure out exactly what I needed to do to achieve what I wanted. Cheers!

  10. Denise says

    This is working perfectly for me; thanks so much!

    However, there’s one thing I can’t quite figure out. The author box now shows for every author, regardless of whether “Enable Author Box on this User’s Posts” is checked or not.

    If the box is un-checked, is there some way to either make that box disappear? I often post news items as “Name of My Site” and don’t need an “About Name of Site” author box to appear below those posts.

    Thanks again for this very useful post — and for any further enlightenment you can provide.