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 year, 3 months ago by
David.
-
AuthorPosts
-
July 21, 2022 at 11:55 pm #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.
July 22, 2022 at 12:16 am #2290294Fernando 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.
July 22, 2022 at 3:25 am #2290424Joey
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.July 22, 2022 at 5:38 am #2290514David
StaffCustomer SupportHi 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.July 22, 2022 at 1:24 pm #2290974Joey
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?
July 22, 2022 at 2:23 pm #2291000Ying
StaffCustomer SupportFor 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.
July 22, 2022 at 8:35 pm #2291088Joey
July 23, 2022 at 3:54 am #2291244David
StaffCustomer SupportYou 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.July 23, 2022 at 8:50 pm #2291730Joey
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.
July 24, 2022 at 3:22 am #2291860David
StaffCustomer SupportHmmm…. 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-2291244Checking your static home page, i don’t see the
author-name
CSS class on the Headline block ?July 24, 2022 at 4:08 am #2291889Joey
I updated the code. The author CSS class goes here, correct?
July 24, 2022 at 11:18 am #2292339Ying
StaffCustomer SupportThat’s correct.
July 24, 2022 at 12:48 pm #2292385Joey
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.
July 25, 2022 at 12:18 am #2292640Joey
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.
July 25, 2022 at 1:09 am #2292678Fernando 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.
-
AuthorPosts
- You must be logged in to reply to this topic.