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 a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

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

Let's Talk

Reader Interactions

Comments

  1. Joshua says

    Bill,

    How would I do this for my Thesis theme with this plugin? Is it pretty similar? I’m hoping to have the multiple authors show up in the fashion of “Author 1, Author 2 and Author 3” or “Author 1 and Author 2” but I’ll settle for “Author 1, Author 2, Author 3”. I just want to make sure I can have all applicable authors show up in the headline.

    Thanks,
    Joshua

    • Bill Erickson says

      Instead of using a filter on genesis_post_info, you could write a function and hook it into thesis_hook_byline_item. Be sure to remove the Author from the byline in Thesis Options first.

      For example (untested code):

      add_action('thesis_hook_byline_item', 'multi_authors');
      function multi_authors() {
      $i = new CoAuthorsIterator();
      $return = '';
      $i->iterate();
      $return .= '<a href="'.get_author_posts_url(get_the_author_meta('ID')).'">'.get_the_author_meta('display_name').'</a>';
      while($i->iterate()){
      $return.= $i->is_last() ? ' and ' : ', ';
      $return .= '<a href="'.get_author_posts_url(get_the_author_meta('ID')).'">'.get_the_author_meta('display_name').'</a>';
      }
      echo $return;
      }
  2. Al says

    Hi Bill,
    thank you for this appreciated tutorial.
    I would display, with coauthor plus plugin, also the list of (both) authors and co-authors in a sidebar, but all my attemps haven’t success. Could you kindly tell me where is the bug?

    I’ve insert in functions.php of my theme


    function get_coauthor_list() {
    global $wpdb;
    $authors = implode("','",get_terms('author',array('fields'=>'names')));
    $sql = "SELECT ID " .
    "FROM {$wpdb->users} " .
    "WHERE user_login IN ('{$authors}') " .
    "ORDER BY display_name";
    return $wpdb->get_col($sql);
    }

    and in the sidebar.php file

    <div class="author" id="author-">

    <a href="">

    but the result is a display of some co-authors and not other authors!

    Do you know a different solution for achieve the target of a complete author/coauthor list?

    Thank you,
    Al

  3. Tony Escobar says

    Hey Bill, thanks for the great tutorial. I really appreciate it.

    Do you have a tutorial or know of any resources that would help me include the secondary authors bio after the post? Basically, display two author bios on a single post (using the Co-Authors Plus plugin and the tutorial you provided above). Thanks!

    • Bill Erickson says

      You’ll need to rebuild the author box function. Use the example code in the first code snippet, but instead of just returning the author name, use the code from Genesis’ author box to display all their information.

    • Bill Erickson says

      The issue is that you’re using it on a page, not a post. You can update the code in my snippet to use is_page() instead of is_single()

  4. Steffen says

    Hi Bill!

    I have a problem with the post info area. I have included the code in my funtions.php file but instead of the author names i see the code “[post_authors_posts_link before=”by “]” in frontend view. Multiple Author boxes works fine.

    I am using the fashionista genesis theme.

    Thanks.

    Steffen

  5. Nick Ciske says

    Bill, thanks for sharing this — it’s exactly what I needed for a project.

    I ran into an issue with the second author box not being displayed because CoAuthors Plus was returning the guest author post type for that user vs. the WP User Object (while it returned the WP User for the Main Author).

    Here’s how I fixed it to handle both cases:
    https://gist.github.com/4428434

  6. TJ Greene says

    Bill,

    I found this page while researching how to replicate some of your handiwork over on Yoast’s site. Specifically, customizing the post info function to go from Genesis’ default of ‘January 8, 2013 by Author’s Name’ to ‘By Author’s Name – Last update October 27th, 2012’ whenever a post is modified. Kudos, it’s BRILLIANT! Just what I’m looking for. I don’t suppose you have a tutorial on it anywhere, or know where I can find some helpful info?

    Anyway, my problem is unrelated to this post, however, I wanted to let you know that your link above to Studiopress’s post on customizing post info (http://dev.studiopress.com/customize-post-info.htm) is broken. That info would be really helpful as I start digging deeper into Genesis. I love their stuff, but it can be confusing to the inexperienced.

    Best Regards,
    TJ Greene

  7. 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!

  8. 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

  9. 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.

      • 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

  10. 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.

  11. 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.

  12. 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!

  13. 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?

  14. 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!

  15. 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.

  16. Paul O'Connor says

    Thanks for the code share Bill. Still working in 2018 with the Genesis Breakthrough theme and Co-Authors Plus.

    I do seem to have to keep Genesis – Simple Edits turned on (with the following entry) otherwise it just displays the default theme post info:

    Entry Meta (above content)
    [post_date] By [post_authors_post_link] [post_comments]

    The only issue I’m having is that I also have Genesis Portfolio-pro installed and the portfolio posts that are assigned to my new co-authors aren’t being registered. So when you click on the author name in a portfolio post (and they aren’t authors that have been assigned to normal blog posts) you just get a 404 error.

    If I have assigned them to other normal blog posts, you get to see them but none of their portfolio posts.

    When I look at the list of Guest Authors, none of their assigned portfolio posts are listed. only their blog posts.

    Am I missing some connection to portfolio-pro?

    Thank you.

Leave A Reply