Your error is that you’ve written an if
inside the sprintf argument list. You can’t do that. Instead, you could use the ternary operator, which has syntax
<condition> ? <result if true> : <result if false>
e.g.
$byline = sprintf(
__('by %s'),
function_exists('coauthors_posts_links')
? ('<span class="author vcard">' .
coauthors_posts_links( null, null, null, null, false ) . '</span>')
: ('<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>')
);
(brackets added for clarity – you probably don’t need them all)
or use a separate variable for the post links and set that first using an if-else, e.g.
if (function_exists('coauthors_posts_links')) {
$author_post_links = coauthors_posts_links( null, null, null, null, false );
} else {
$author_post_links="<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>';
}
$byline = sprintf(
__('by %s'),
'<span class="author vcard">' . $author_post_links . '</span>' );
As discussed in comments
- by default
coauthors_posts_links()
will echo the links as well as returning them as a string, which is not what you want here. You’ll need to pass $echo = false, which is the fifth argument, so we’ll need to fill in the previous four with default null values. - I don’t think you want to
esc_html()
the output ofcoauthors_post_links()
so I’ve removed that too.