[Resolved] Two authors in a single post

Home Forums Support [Resolved] Two authors in a single post

Home Forums Support Two authors in a single post

Viewing 15 posts - 1 through 15 (of 30 total)
  • Author
    Posts
  • #1292254
    Sagar

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

    #1292665
    David
    Staff
    Customer Support
    #1292711
    Sagar

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

    #1292880
    David
    Staff
    Customer Support

    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…

    #1293182
    Sagar

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

    #1293338
    David
    Staff
    Customer Support

    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.

    #1294051
    Sagar

    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)

    #1294487
    Tom
    Lead Developer
    Lead Developer

    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>'; 
    } );
    #1298345
    Sagar

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

    #1298593
    Tom
    Lead Developer
    Lead Developer

    Yes 🙂

    #2008481
    Louis-Xavier

    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!

    #2008513
    Ying
    Staff
    Customer Support

    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 🙂

    #2008554
    Louis-Xavier

    Now it works exactly as I wanted it to.

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

    #2008603
    Ying
    Staff
    Customer Support

    You are welcome 🙂

    #2208405
    Rupert

    Hey 🙂

    Does the above snippet still work?

    I couldn´t get it working, unfortunately.

    Any help would be much appreciated!

Viewing 15 posts - 1 through 15 (of 30 total)
  • You must be logged in to reply to this topic.