Getting coauthors plus to show coauthors on post

Hi,

I recently downloaded and activated the Coauthors plugin. I would like to know how to edit my child theme files to make the coauthors to appear at the top of each post. The only related message I found related was associated with the Fashionistas theme (Co-Authors Plus Template Tag).

Roughly following the advice you gave on that topic, I edited the function sydney_posted_on() in my Sydney Pro Child theme functions.php file (I have posted the function below) and replaced this line:
esc_html( get_the_author() )
with
coauthors_posts_links()

This produces a duplicate of the coauthors under the title of the post. Could you tell me how to modify the below code to avoid the duplication please?

That would be a great help!
Cheers
Tilla

function sydney_posted_on() {
$time_string = ‘%2$s’;
if ( get_the_time( ‘U’ ) !== get_the_modified_time( ‘U’ ) ) {
$time_string = ‘%2$s%4$s’;
}

$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(
	_x( '%s', 'post date', 'sydney' ),
	'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
);

$byline = sprintf(
	_x( '%s', 'post author', 'sydney' ),
	'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);

echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; 

if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
	echo '<span class="comments-link">';
	comments_popup_link( __( 'Leave a comment', 'sydney' ), __( '1 Comment', 'sydney' ), __( '% Comments', 'sydney' ) );
	echo '</span>';
}

$categories_list = get_the_category_list( __( ', ', 'sydney' ) );
if ( $categories_list && sydney_categorized_blog() ) {
	 printf( '<span class="cat-links">' . __( '%1$s', 'sydney' ) . '</span>', $categories_list ); 
	/*	printf( $categories_list );  Using this removes the category widget*/
}

}

Hello there,

Try replacing this line:

   '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'

with:

   '<span class="author vcard">'.coauthors_posts_links().'</span>'

Regards,
Kharis

Hi, Thanks for getting back to me. Unfortunately I still get duplicates! See…

Hello there,

I tried it on my test site and it works fine. Does your final code look like this?

    function sydney_posted_on() {
    	$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(
    		_x( 'Posted %s', 'post date', 'sydney' ),
    		'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
    	);

    	$byline = sprintf(
    		_x( '%s', 'post author', 'sydney' ),
    		'<span class="author vcard">'.coauthors_posts_links().'</span>'
    	);

    	echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>';

    	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
    		echo '<span class="comments-link">';
    		comments_popup_link( __( 'Leave a comment', 'sydney' ), __( '1 Comment', 'sydney' ), __( '% Comments', 'sydney' ) );
    		echo '</span>';
    	}

    	$categories_list = get_the_category_list( __( ', ', 'sydney' ) );
    	if ( !is_single() && $categories_list && sydney_categorized_blog() ) {
    		printf( '<span class="cat-links">' . __( 'Posted in %1$s', 'sydney' ) . '</span>', $categories_list );
    	}
    }

Regards,
Kharis

Hi again Kharis,

I have no idea why it worked for you using the above code. The default setting of the function coauthors_posts_links() is coauthors_posts_links(null,null,null,null,true). ‘True’ means * the co-authors should will be echoed rather than returned…hence why I was getting duplicates of the coauthors.

I am now using this code which works and also adds a safeguard in case the plugin coauthors plus is deactivated. Hopefully :wink:

    if ( function_exists( 'coauthors_posts_links' ) ) {
       	$byline = sprintf(
    		_x( '%s', 'post author', 'sydney' ),
    		'<span class="author vcard">'.coauthors_posts_links(null,null,null,null,false).'</span>'
    	);
    } else {
        $byline = sprintf(
    		_x( '%s', 'post author', 'sydney' ),
    		'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
    	);
    }

Thanks again for your feedback and I will have more questions soon :smiley:

Great!

You’re welcome! Please let us know in a new topic if you have any further questions, or if we can provide you with any other assistance.

Regards,
Kharis