Site logo

Archived topic

Two authors in a single post

29 replies · Started by Sagar on May 19, 2020

Viewing posts 1–15 of 30

Is it possible to add contributors or co-author in a post using hero meta?

So there is not a solution to add co-authors without using the plugin?

You could use Custom Fields to add the second author name.

Then use the generate_post_author_output filter to include the second name:

https://docs.generatepress.com/article/generate_post_author_output/

For example:

add_filter( 'generate_post_author_output', function( $output ) {  
    $coauthor = get_post_meta( get_the_ID(), 'your_coauthor_custom_field_name', true );
    return $output . '<span class="coauthor"> and ' . $coauthor . '</span>'; 
} );

replace this your_coauthor_custom_field_name with the custom field name you have used.

This will only display the author name in the byline - it won't add any links, create a new author page or add it to meta etc...

Hello. I have added the filter on a custom field, but that didn't work for me.
Entered custom field
Sample post

lets start again.
1.1 Create a new custom field.
1.2 Give it a name of coauthor
1.3 Give it a value e.g David

2.1 Now in the code above change this your_coauthor_custom_field_name for coauthor
2.2 Now add this code to your site following these instructions:

https://docs.generatepress.com/article/adding-php/

Whenever you want to add Co author to a new post simply select the coauthor field and give it a value.

Thank you.
Now I need to add the author link to that particular text.
For example: Written By Sagar Regmi and Medically reviewed by (co author name with link)

You would have to add a second custom field with the URL, then do this:

add_filter( 'generate_post_author_output', function( $output ) {  
    $coauthor = get_post_meta( get_the_ID(), 'your_coauthor_custom_field_name', true );
    $coauthor_link = get_post_meta( get_the_ID(), 'your_coauthor_link_custom_field_name', true );

    return $output . '<span class="coauthor"> and <a href="' . $coauthor_link . '">' . $coauthor . '</a></span>'; 
} );

Do I need to replace this code to the code that David had provided?

Yes :)

Hi

Give this snippet a try:

add_filter( 'generate_post_author_output', function( $output ) {  
    $coauthor = get_post_meta( get_the_ID(), 'your_coauthor_custom_field_name', true );
    $coauthor_link = get_post_meta( get_the_ID(), 'your_coauthor_link_custom_field_name', true );
    if(empty($coauthor)){
	    return $output;
    }
    return $output . '<span class="coauthor"> and <a href="' . $coauthor_link . '">' . $coauthor . '</a></span>'; 
} );

Let me know :)

Now it works exactly as I wanted it to.

Thank you very much Ying! That's all I needed.

You are welcome :)

Hey :)

Does the above snippet still work?

I couldn´t get it working, unfortunately.

Any help would be much appreciated!

This archived topic is closed to new replies.