[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

  • This topic has 30 replies, 4 voices, and was last updated 1 month ago by David.
Viewing 15 posts - 16 through 30 (of 31 total)
  • Author
    Posts
  • #2293486
    Joey

    Fernando,

    That code was used for the blog home and archives like Categories, which just have the default feed. In other words, all posts. To be honest, I just copied and pasted that code from the forums, but it worked well until I tried to but these two posts on the static featured page.

    The other issue with deleting the original code is that my posts have a page hero header that shows the author: {{post_author}} | {{post_date}}. I’m thinking, I could build the home blog page with GerenateBlocks instead of the default feed, and put the different categories on the same page. Then it would have the GB headline block and I could delete that first code, but it would still say “Guest Author” in the page hero when users go to the actual post.

    #2293528
    Fernando
    Customer Support

    I see. If that’s the case, just add a conditional statement to your code so that it works only on default feeds.

    For example, if your blog and search pages are “default”:

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

    Just alter the conditional statement so it works only in your preferred pages, and it doesn’t affect others.

    Hope this clarifies!

    #2293553
    Joey

    Oh, thank you Fernando, I didn’t even think of that. I also added is_single() so that it would display the proper name in the page hero of the actual post and is_archive() for the category pages. Everything is working really well now, on both the excerpts from the featured page and all the blog pages. Thank you everyone so much, and sorry for all the questions. It looks really good and is starting to look a lot more professional. Thanks again.

    #2293558
    Fernando
    Customer Support

    You’re welcome Joey! Glad it’s working now!

    #2316219
    Joey

    Sorry to bring this back up, but I’m having issues with “Guest Author” appearing again. I’m trying to replace the featured post on my blog home with a block element that has a query loop in it (see the example below).

    This code:

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

    is conflicting with Dave’s 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 );

    because is_home() is where I am trying to place the block element. Is the a way to specify either that 1) function guest_author_name doesn’t affect the block element on that page, or 2) function db_guest_author_removal doesn’t affect anything outside the block element?

    I don’t know if this is possible: the issue is that before I was doing the featured image on my blog home with CSS, but it’s getting to be too much conflicting code, and if I can do this with a header element instead there are a lot more options for customizing the featured post and making it responsive. But if it displays “Guest Author” in addition to the person’s name, I don’t know if it will work. But I thought I’d ask. Thank you.

    #2316427
    David
    Staff
    Customer Support

    Is the second Headline that displays the Guest Author actually required on that page ? What if you remove that from that specific query loop block.

    #2316904
    Joey

    The top post (that displays “Guest Author”) is the query loop. The one below (that doesn’t display “Guest Author”) is the featured post. The idea is to remove the featured post and leave the query loop.

    I was doing this with CSS on the featured post instead, and was getting interference, too much CSS is on the site. I know I can clean it up, but If I can do this with the query loop it’s just a simple solution and is easily customizable—easy to make it responsive, can add lists or customize the featured post, include other images, etc.

    So if it won’t work or is a lot of trouble, that’s okay. But if there is an easy way to make the PHP work with a query loop, it would be a better solution. WordPress doesn’t work well with a lot of guest authors, that’s been the root of the problem. I thought I’d ask—this is the last issue on the site. Thanks for your help.

    #2317336
    David
    Staff
    Customer Support

    Sorry what i meant was – is the guest-author headline block in the Query Loop even required?

    #2317979
    Joey

    Oh, sorry, I understand. “Guest Author” is the name of the dummy user. For example, if the real writer’s name is “Joey Carney”, it would display “by Ayi Kwei ArmahJoey Carney” (“Ayi Kwei Armah” is the guest-author data I added to custom fields). I couldn’t delete the guest-author meta because because many of the posts are by writers who aren’t real users on the site. Is that what you are asking?

    But I’m testing it a little more, and apparently I can add a guest-author to posts no matter who the real user is who creates the post. I didn’t know that. But since the author name isn’t linked on the site, if I always add a guest-author to custom fields, then I guess I can just delete the author query in the query loop, and leave the guest-author meta, and it should work.

    The only disadvantage is that the writer would always have to put in guest-author, even when the person creating the post is a registered user on the site. But that’s the only solution I can think of without using code (if that’s possible).

    #2319783
    David
    Staff
    Customer Support

    Hmm…. probably my brain is having a moment.
    So could you humour me and let me know if this is the process.

    1. Any posts written by guest author:
    a) are published under the Guest Author user.
    b) have a custom field containing the guest authors display name

    2. When we display a post we want to decided if the author is a Guest Author.
    This can be determined in one of two ways:

    i) If the Author Name ( get_the_author ) = ‘Guest Author’
    ii) If the get_post_meta( $post->ID, 'guest-author', true ); contains a value

    3. If it is a Guest author then the author meta should display the value from the get_post_meta ( point ii) above ).

    4. Currently ALL posts under the Guest Author have a get_post_meta value.

    5. Currently ALL posts with a regular Author name do not have get_post_meta value.

    Is that correct?

    #2321863
    Joey

    Yes, that is correct on all the points above, except that it doesn’t matter who the real author is. I had created a “Guest Author” account because I had thought that was a prerequisite for adding guest-author meta data, but it’s not. I could delete that account. But as of now, all posts with a guest-author meta are written by the “Guest Author” user.

    Just to make it clear, this is the block element at the top of the blog. And this is what it looks like on the blog home. It was written by a user named “Guest Author” and when editing the post they added “Ayi Kwei Armah” in custom fields under guest-author. However, if the user is named “Joey Carney”, also with “Ayi Kwei Armah” in custom fields under guest-author, it displays this. No matter what user wrote it, it displays this on the regular blog feed, when it isn’t a block element, as it should (the real user name is invisible). That is also what it is supposed to do in the block element at the top of the page, but it doesn’t.

    Now, if there is no guest-author data in custom fields, it works fine: the guest-author meta is invisible like it should be. Sometimes users post their own articles. The trouble is that when there is custom fields guest-author meta in the post, it shows the real user name on the block element (query loop), instead of being invisible.

    I should add that the block element is a content template applied to the first post (featured image). Sorry, I’m not explaining it well. It’s kind of a confusing process.

    #2322134
    David
    Staff
    Customer Support

    So should the guest-author custom field name only be displayed if the user is logged in as the Guest Author?

    Or are there valid instances where a named author would like to display the guest-author custom field name in place of their author name ?

    As an aside, having the Guest Author account does have its advantages, as at least its easy to see all guest posts in one place. So i personally would keep that.

    #2322930
    Joey

    David,

    A named author doesn’t need to use the guest-author custom field, but whatever is easiest. Right now there are only 2 or 3 users now that regularly create posts, but we’ll keep growing. I’m just trying to make it as automated as possible so it’s simple for users and administrators. I agree that it would be easier if guest-author custom field is only for the Guest Author account. Either way, if you know a simple solution, I don’t want to take too much of your time. Thanks for all your help.

    #2323445
    David
    Staff
    Customer Support

    Ok, for now, if you were to edit the Query Loop or any Content Templates where you have the 2 x headline blocks, one showing the Author name and the other showing the guest-author custom field.
    And for each of those blocks give them a CSS Class of author-meta

    Then add this CSS:

    .author-meta + .author-meta {
        display: none !important;
    }

    It should work as long as theres nothing else between the two blocks.

    #2323766
    Joey

    Wow, that’s a really simple solution, I didn’t know I could do that. I removed this PHP and the CSS class that went with it, since it isn’t necessary now:

    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 );

    Thanks a lot for your help again.

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