[Resolved] Creating a custom filter to over-ride the post author meta

Home Forums Support [Resolved] Creating a custom filter to over-ride the post author meta

Home Forums Support Creating a custom filter to over-ride the post author meta

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1404874
    Ryan

    Hi there,

    I am using Advanced Custom Fields in my site to make custom post types in a few areas.
    The issue that I am having is that I have setup a custom field within my blog posts named “story contributor”. What I want to do is have it replace the default post author coming up as the person who published the blog post (sort of like a guest author). So I went through the GP documentation about using filters:

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

    and came up with the following code to detect if poster put the guest contributor name in the acf field and have it overwrite on the post meta:

    /* Filter to modify the post metadata (author) */
    add_filter( 'generate_post_author_output', function( $output ) {  
        if (get_field('story_contributor')) :
            $story_author = the_field('story_contributor');
            return ' | by <i class="fa fa-user-circle" aria-hidden="true"></i>' . $story_author;
        else :
            return ' | <i class="fa fa-user-circle" aria-hidden="true"></i> ' . $output;
        endif;
    } );

    However, the front end is coming out very strange, whereby the “story author” comes out in front of the fontawesome icon and other precursor text, like this:

    July 18, 2020 Jeff Spears | by [font-icon]

    How can I amend the code to make sure the custom filter prints out in the right order?
    The default behaviour works fine as expected (when no story contributor is inserted into blog post).

    #1404982
    David
    Staff
    Customer Support

    Hi there,

    thats because the_field() function outputs the value, whereas get_the_field() returns it.

    Instead you would do something like this:

    $story_author = get_field('story_contributor');
    if ( $story_author ) {
        return ' | by <i class="fa fa-user-circle" aria-hidden="true"></i>' . $story_author;
    }
    #1413618
    Ryan

    Thanks for the code suggestion. Did the trick (with a few little customizations). Much more simpler than my previous attempt.

    #1413778
    David
    Staff
    Customer Support

    Glad to hear that

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