-
Notifications
You must be signed in to change notification settings - Fork 205
Template tags
If you've just installed Co-Authors Plus, you might notice that the bylines are being added in the backend but aren't appearing on the frontend. You'll need to add the template tags to your theme before the bylines will appear.
WordPress offers template tags like the_author() and the_author_posts_link() to display byline information associated with each post. You might see these inside files like single.php and author.php.
Co-Authors Plus has similar template tags for displaying multiple bylines. These are located in the template-tags.php file. Use the Co-Authors Plus template tags to list co-authors anywhere you'd normally list the author.
Each of these template tags will present different aspects of the multiple bylines. For instance, the first will display the first and last name of each co-author without any links. The second will display the first and last name of each co-author linking back to their author profile page. And so on.
/**
* Outputs the co-authors display names, without links to their posts.
* Co-Authors Plus equivalent of the_author() template tag.
*
* @param string $between Delimiter that should appear between the co-authors
* @param string $betweenLast Delimiter that should appear between the last two co-authors
* @param string $before What should appear before the presentation of co-authors
* @param string $after What should appear after the presentation of co-authors
* @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
*/
function coauthors( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ){
return coauthors__echo( 'display_name', 'field', array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after
), null, $echo );
}
/**
* Outputs the co-authors display names, with links to their posts.
* Co-Authors Plus equivalent of the_author_posts_link() template tag.
*
* @param string $between Delimiter that should appear between the co-authors
* @param string $betweenLast Delimiter that should appear between the last two co-authors
* @param string $before What should appear before the presentation of co-authors
* @param string $after What should appear after the presentation of co-authors
* @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
*/
function coauthors_posts_links( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ){
return coauthors__echo('coauthors_posts_links_single', 'callback', array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after
), null, $echo );
}
/**
* Outputs the co-authors display names, with links to their websites if they've provided them.
*
* @param string $between Delimiter that should appear between the co-authors
* @param string $betweenLast Delimiter that should appear between the last two co-authors
* @param string $before What should appear before the presentation of co-authors
* @param string $after What should appear after the presentation of co-authors
* @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
*/
function coauthors_links($between = null, $betweenLast = null, $before = null, $after = null, $echo = true ) {
return coauthors__echo('coauthors_links_single', 'callback', array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after
), null, $echo );
}
To integrate Co-Authors Plus, you’ll want to replace existing author template tags in your theme with a simple conditional that uses the Co-Authors Plus template tags if Co-Authors Plus is available. The conditional prevents your site from breaking if Co-Authors Plus isn’t activated.
For example, here’s how you would update the_author_posts_link()
to instead use coauthors_posts_links()
:
if ( function_exists( 'coauthors_posts_links' ) ) {
coauthors_posts_links();
} else {
the_author_posts_link();
}
However, the example above is a relatively simplistic way of presenting bylines. There’s a good chance your theme will need an adaptation of it.
For instance, here’s how the change looks for the Hybrid theme:
function hybrid_entry_author_shortcode( $attr ) {
$attr = shortcode_atts(
array(
'before' => '',
'after' => '',
),
$attr
);
if ( function_exists( 'coauthors_posts_links' ) ) {
$author = coauthors_posts_links( null, null, null, null, false );
} else {
$author = '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '" title="' . esc_attr( get_the_author_meta( 'display_name' ) ) . '">' . get_the_author_meta( 'display_name' ) . '</a></span>';
}
return $attr['before'] . $author . $attr['after'];
}
Here’s the new function for TwentyTen:
if ( ! function_exists( 'twentyten_posted_on' ) ) :
/**
* Integrate Co-Authors Plus with TwentyTen by replacing twentyten_posted_on() with this function.
*/
function twentyten_posted_on() {
if ( function_exists( 'coauthors_posts_links' ) ) :
printf(
__( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'twentyten' ),
'meta-prep meta-prep-author',
sprintf(
'<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
get_permalink(),
esc_attr( get_the_time() ),
get_the_date()
),
coauthors_posts_links( null, null, null, null, false )
);
else:
printf(
__( '<span class="%1$s"<Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'twentyten' ),
'meta-prep meta-prep-author',
sprintf(
'<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
get_permalink(),
esc_attr( get_the_time() ),
get_the_date()
),
sprintf(
'<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
get_author_posts_url( get_the_author_meta( 'ID' ) ),
esc_attr( sprintf( __( 'View all posts by %s', 'twentyten' ), get_the_author() ) ),
get_the_author()
)
);
endif;
}
endif;
For the Neve Theme, add this to your child-theme functions.php:
add_filter( 'the_author_posts_link', function( $markup ) {
if ( ! function_exists( 'coauthors_posts_links' ) ) {
return $markup;
}
ob_start();
coauthors_posts_links();
$output = ob_get_contents();
ob_end_clean();
return $output;
} );
Use the coauthors_wp_list_authors()
template tag. This accepts many of the same arguments as wp_list_authors()
. Look in template-tags.php for more details.
To ensure posts with your CPT are counted, use the coauthors_count_published_post_types
filter.
add_filter( 'coauthors_count_published_post_types', function( $post_types ) {
$post_types[] = 'my_cpt_slug';
return $post_types;
} );
To display the metabox on your CPT, either call register_post_type()
with $args['supports']
containing 'author'
, or call add_post_type_support( 'my_cpt_slug', 'author' );