Site logo

[Support request] Add 2nd author to Posts and Pages

Home Forums Support [Support request] Add 2nd author to Posts and Pages

Home Forums Support Add 2nd author to Posts and Pages

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #2429550
    Wayne

    In another support topic the poster requested instruction to add a Facts Checked By field in addition to Author By in element block.
    The response was:

    ou will need to create a Custom Field on your site.
    You can use the WP core ones: https://wordpress.org/support/article/custom-fields/
    Or a plugin like ACF to add them.

    You can then edit the Block Element you’re using to display your Post Meta, and include another Headline block to show your Custom Field by choosing the Post Meta option in the blocks dynamic data.

    Is there a way to make a condition that the second author (who will not be posting or publishing their own articles – Just “Facts Checked By” headline) does not appear on the Element block if the Value field is empty in the page or post?

    Thank you!

    #2429579
    David
    Staff
    Customer Support

    Hi there,

    theres a couple of options, one of which would be to filter in the Facts Checked by label to the headline block.

    1. Add this PHP Snippet:

    
    add_filter( 'render_block', function( $block_content, $block ) {
    	
        if (
        !is_admin() 
        && ! empty( $block['attrs']['className'] )
        && 'fact-checker' === $block['attrs']['className']
        ){
            $block_content = '<span class="label">Facts Checked by <span>' . $block_content;
        }
        
        return $block_content;
    
    }, 10, 2 );

    2. Select the Block that is set to Display the second author and in its Advanced > Additional CSS class(es) add: fact-checker

    The snippet about will then add: <span class="label">Facts Checked by <span> in front of the author if it exists.

    #2429630
    Wayne

    I added the snippet to Snippets plugin and I get the following error:

    Don’t Panic
    The code snippet you are trying to save produced a fatal error on line 8:

    syntax error, unexpected ‘;’
    The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.

    Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.

    This is a great solution if we can get working!

    #2429643
    David
    Staff
    Customer Support

    Oh damn, i am really really sorry about that. I updated the code above to add the missing )

    https://generatepress.com/forums/topic/add-2nd-author-to-posts-and-pages/#post-2429579

    #2429748
    Wayne

    Thanks! So it does display the label, but not the author name.

    I have edited the element that is to display on the page. I have the custom field with the correct key and value. Site URL in the Private Info.

    Also, I removed the author name in the Value field of Custom Field and saved, but the label was still shown, even though the Value was blank.

    Am I missing something?

    Thanks!

    #2429924
    Ying
    Staff
    Customer Support

    Also, I removed the author name in the Value field of Custom Field and saved, but the label was still shown, even though the Value was blank.

    I’m not seeing the value or the lable on the front end.
    https://www.screencast.com/t/VDaEa3Uo

    Can you add the author name back and let us take a look?

    #2429939
    Wayne

    I added that back in.

    #2429985
    Ying
    Staff
    Customer Support

    If you don’t activate the custom function, does the value show on front end?

    #2430022
    Wayne

    For the custom field?

    #2430101
    Ying
    Staff
    Customer Support

    Yes.

    #2430117
    Wayne

    I don’t have the custom field activated for other pages, but the label still shows.

    #2430121
    Wayne

    I am just going to take that off for now. Thanks.

    #2431975
    Ying
    Staff
    Customer Support

    I don’t have the custom field activated for other pages, but the label still shows.

    The label is added to that headline output, so even if the value is empty, the label will still show.

    A fix for this would be modifying David’s code a little bit, change second_author to your post meta field slug.

    add_filter( 'render_block', function( $block_content, $block ) {
        $second_author = get_post_meta( get_the_ID(), 'second_author', true );
        if (!is_admin() && ! empty( $block['attrs']['className'] ) && 'medically_reviewed_by' === $block['attrs']['className'] && ! empty($second_author)){
            $block_content = 'Medically reviewed by ' . $block_content;
        }
        return $block_content;
    }, 10, 2 );
Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.