- This topic has 29 replies, 5 voices, and was last updated 1 year, 8 months ago by Ying.
-
AuthorPosts
-
May 19, 2020 at 8:58 am #1292254Sagar
Is it possible to add contributors or co-author in a post using hero meta?
May 19, 2020 at 1:50 pm #1292665DavidStaffCustomer SupportHi there,
Theres a solution here if using the Co-authors plugin:
May 19, 2020 at 2:25 pm #1292711SagarSo there is not a solution to add co-authors without using the plugin?
May 19, 2020 at 4:40 pm #1292880DavidStaffCustomer SupportYou 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…
May 19, 2020 at 11:18 pm #1293182SagarHello. I have added the filter on a custom field, but that didn’t work for me.
May 20, 2020 at 1:49 am #1293338DavidStaffCustomer Supportlets start again.
1.1 Create a new custom field.
1.2 Give it a name ofcoauthor
1.3 Give it a value e.gDavid
2.1 Now in the code above change this
your_coauthor_custom_field_name
forcoauthor
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.
May 20, 2020 at 9:02 am #1294051SagarThank 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)May 20, 2020 at 2:24 pm #1294487TomLead DeveloperLead DeveloperYou 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>'; } );
May 23, 2020 at 10:56 am #1298345SagarDo I need to replace this code to the code that David had provided?
May 23, 2020 at 4:45 pm #1298593TomLead DeveloperLead DeveloperYes 🙂
November 17, 2021 at 1:56 pm #2008481Louis-XavierHi 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!
November 17, 2021 at 2:59 pm #2008513YingStaffCustomer SupportHi
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 🙂
November 17, 2021 at 3:38 pm #2008554Louis-XavierNow it works exactly as I wanted it to.
Thank you very much Ying! That’s all I needed.
November 17, 2021 at 4:37 pm #2008603YingStaffCustomer SupportYou are welcome 🙂
May 3, 2022 at 6:01 pm #2208405RupertHey 🙂
Does the above snippet still work?
I couldn´t get it working, unfortunately.
Any help would be much appreciated!
-
AuthorPosts
- You must be logged in to reply to this topic.