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