Showing Co-Authors on post page

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.

2 Answers
2

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 of coauthors_post_links() so I’ve removed that too.

Leave a Comment