Archived topic
Two authors in a single post
29 replies · Started by Sagar on May 19, 2020
Is it possible to add contributors or co-author in a post using hero meta?
Hi there,
Theres a solution here if using the Co-authors plugin:
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.


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 guys,
Followed the path to success here, works fine.
Only thing is every post now shows as if there's a coauthor... as you can see here: https://www.topolocal.ca/2021/11/06/on-ne-devrait-pas-ecrire-sur-la-politique-avec-des-gants-de-hockey/
There must be a function to revert to standard if custom post field is not active but I can't figure it out by myself.
If you could help me out here, I'd be very happy!
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!