I am trying to make my theme compatible with Co-Authors Plus plugin and show the co-authors of a post if had any.
I spent about 30 minutes trying to convince myself that I understood the following code to display post author and date:
function posted_on() {
global $post;
$author_id=$post->post_author;
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
$posted_on = sprintf(
__( '<i class="entry-date">'. get_the_date( 'F d, Y' ) .'</i>' ),
'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
);
$byline = sprintf (
__( 'by %s' ),
'<span class="author vcard"><a class="url fn n" href="' . esc_url(
get_author_posts_url( $author_id )
) . '">' . esc_html( get_the_author_meta( 'display_name', $author_id ) ) . '</a>' . '</span>'
);
echo '<span class="posted-on">' . $posted_on . '</span><i><span class="byline"> ' . $byline . '</span></i>'; // WPCS: XSS OK.
}
So I added a function:
$byline = sprintf(
__('by %s'),
/*Added this*/
if (function_exists('coauthors_posts_links')) {
'<span class="author vcard">'.esc_html(coauthors_posts_links()).
'</span>'
} else {
'<span class="author vcard"><a class="url fn n" href="'.esc_url(
get_author_posts_url($author_id)
).
'">'.esc_html(get_the_author_meta('display_name', $author_id)).
'</a>'.
'</span>'
}
);
The function was supposed to work like…
IF THIS POST POST CONTAINS CO-AUTHORS,
DISPLAY AUTHORS AND CO-AUTHORS.
IF POST DOESN’T CONTAIN CO-AUTHORS,
SHOW THE DEFAULT AUTHOR LINK.
But that just made error. Could use a little advice.