[Resolved] Link to posts after heading in a static page using query loop – guest authors

Home Forums Support [Resolved] Link to posts after heading in a static page using query loop – guest authors

Home Forums Support Link to posts after heading in a static page using query loop – guest authors

Viewing 15 posts - 1 through 15 (of 31 total)
  • Author
    Posts
  • #2290278
    Joey

    I’m trying to put a really basic featured image + author + excerpt of 2 recent posts in the middle of a static page. I used GenerateBlocks with dynamic data to replicate them with links and it worked fine. The trouble is that I use custom fields to create guest-author names on many of the posts and the custom field isn’t showing up in this query loop.

    In other words, since we have a lot of guest writers who only contribute once, rather than create a new dummy contributor on wordpress every time, I can use the “Guest Writer” contributor and create a custom field with the following code. On the blog home and on the actual post, it shows the writer’s name instead of “Guest Writer”. But on the 2 recent posts on the static page, it shows “Guest Writer”.

    add_filter( 'the_author', 'guest_author_name' );
    add_filter( 'get_the_author_display_name', 'guest_author_name' );
     
    function guest_author_name( $name ) {
    global $post;
     
    $author = get_post_meta( $post->ID, 'guest-author', true );
     
    if ( $author )
    $name = $author;
     
    return $name;
    }

    I know I can just manually put in the writer’s name within the container, but I’m trying to automate it. I mean, I could reformat the 2 posts without the author, but is there a way I can do this with the author where it will show the custom field instead of “Guest Author”? It’s under the heading “Magazine” in the link below. Thank you.

    #2290294
    Fernando
    Customer Support

    Hi Joey,

    GB Headline Blocks are capable of retrieve post meta data like texts. Just enable Dynamic data in the Block’s settings, set the source to post meta, and the field to the respective field name.

    Through this, you shouldn’t need code.

    Kindly let us know.

    #2290424
    Joey

    Okay, so if I select post meta, and then write in guest-author, it works for the posts that have a guest author, but it’s blank for posts that have a real author. Since these 2 posts are showing the most recent posts, sometimes it will have real authors and sometimes guest authors. I could force all posts to have a guest author with the custom field, but then things like author pages wouldn’t work for the regular contributors.

    #2290514
    David
    Staff
    Customer Support

    Hi there,

    add 2 x headline blocks, one to display your custom field guest author and one for displaying the core Author name.
    Which ever one is empty will be removed.

    #2290974
    Joey

    Okay, so when I put both headline blocks, one with the core author and one with the metadata, it works for the posts that have a real author (the metadata is invisible), but on the one with a guest author it shows both. For example, “by Guest Author Stephen King” or “by Stephen King Guest Author” instead of just “by Stephen King” since the display name is “Guest Author”. It appears that settings don’t let me remove the display name?

    #2291000
    Ying
    Staff
    Customer Support

    For example, “by Guest Author Stephen King” or “by Stephen King Guest Author” instead of just “by Stephen King” since the display name is “Guest Author”.

    Can you take a screenshot of the dynamic data setting section of the headline block?

    You can upload the screenshot here: https://postimages.org/ and share the link with us.

    #2291088
    Joey
    #2291244
    David
    Staff
    Customer Support

    You can try this:

    1. Select the Author name block the one showing the Display name. And in Advanced > Additional CSS Class(es) add: author-name

    2. Add this PHP Snippet:

    function db_guest_author_removal( $block_content, $block ) {
        $author_block = get_the_author();
        if ( ! empty( $block['attrs']['className'] ) && 'author-name' === $block['attrs']['className'] && 'Guest Author' === $author_block ) {
            $block_content = '';
        } 
        return $block_content;
    }
     
    add_filter( 'render_block', 'db_guest_author_removal', 10, 2 );

    This should check if the current authors display name = Guest Author. If it is then return nothing in its place.

    #2291730
    Joey

    Thanks David, it’s getting close. If I remove my original code:

    add_filter( 'the_author', 'guest_author_name' );
    add_filter( 'get_the_author_display_name', 'guest_author_name' );
     
    function guest_author_name( $name ) {
    global $post;
     
    $author = get_post_meta( $post->ID, 'guest-author', true );
     
    if ( $author )
    $name = $author;
     
    return $name;
    }

    And replace it with your code:

    function db_guest_author_removal( $block_content, $block ) {
        $author = get_the_author();
        if ( ! empty( $block['attrs']['className'] ) && 'author-name' === $block['attrs']['className'] && 'Guest Author' === $author ) {
            $block_content = '';
        } 
        return $block_content;
    }
     
    add_filter( 'render_block', 'db_guest_author_removal', 10, 2 );

    Then it is successful and removes “Guest Author” on the static page, leaving the name I added to custom fields as it should. However, that causes the blog home and archives like Categories to display “Guest Author” instead of the name.

    If I leave both codes, the blog home and categories work as they should, but it has no effect on the static page—it still says “Name Guest Author”. I’ll put a link to both pages below. Currently both codes are active.

    #2291860
    David
    Staff
    Customer Support

    Hmmm…. i made an amend to my code, switching out $author to $author_block just to avoid a variable clash, although id doubt its an issue it gives me some piece of mind.
    https://generatepress.com/forums/topic/link-to-posts-after-heading-in-a-static-page-using-query-loop-guest-authors/#post-2291244

    Checking your static home page, i don’t see the author-name CSS class on the Headline block ?

    #2291889
    Joey

    I updated the code. The author CSS class goes here, correct?

    #2292339
    Ying
    Staff
    Customer Support

    That’s correct.

    #2292385
    Joey

    Hmmmm, I’ll go through the code again and make sure I didn’t miss something. I’m not sure why it’s not working.

    #2292640
    Joey

    David, if I’m understanding you right, author-name is in the Headline block. The fact that it’s working when I remove my original code makes me think that there must be something interfering with it from there. But I’m just guessing.

    If this won’t work, I could try changing the “Guest Author” username to something unnoticeable like a period, or it would probably be better to just get rid of the author completely on these excerpts. But if would be nice if it looked like the posts in the blog. Do you have any other ideas?

    Thank you to everyone for your help so far.

    #2292678
    Fernando
    Customer Support

    Hi Joey,

    To clarify, since you’re already using a GB Headline Block to retrieve to Author and Guest Author, why do we still need this code?:

    add_filter( 'the_author', 'guest_author_name' );
    add_filter( 'get_the_author_display_name', 'guest_author_name' );
     
    function guest_author_name( $name ) {
    global $post;
     
    $author = get_post_meta( $post->ID, 'guest-author', true );
     
    if ( $author )
    $name = $author;
     
    return $name;
    }

    Is it intended for a specific page in your site that isn’t using the Headline Block to retrieve the authors?

    Kindly let us know.

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