Archived topic
Load Author Box (block element) on posts and pages based on author ID
8 replies · Started by Incal on June 20, 2022
Hi,
I found this code in another support topic:
add_filter( 'generate_block_element_display', function( $display, $element_id ) {
$author_id = get_post_field( 'post_author', get_the_ID() );
if ( 553 === $element_id && '2' == $author_id ) {
$display = true;
} else if ( 553 === $element_id && '2' != $author_id ){
$display = false;
}
return $display;
}, 15, 2 );
It works for posts but the element doesn't load on pages. Could you please let me know what I can tweak to load my author box on posts and pages for the selected author?
Many thanks,
Kevin
Hi Kevin,
That code should work for pages as well.
Tested the code as well to be sure and it works as expected from my end.
Can you try clearing all caching mechanism set in your website? Can you also share the link to a page where the Block Element should be appearing?
You may use the private information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
Hope to hear from you soon. :)
Hi Fernando,
Thank you for the quick reply.
On second look, it seems that the element is loading but the author box doesn't appear on pages. It works fine on posts though.
Please see examples in the private section.
Many thanks,
Kevin
Also, one thing I noticed is that the block appears also in archive pages (e.g. blog). Would it be possible for it to appear only on single posts and pages?
Thank you,
Kevin
Hi there,
quick question - are you wanting a different author box each author ?
Hi David,
Yes, because pretty much each author has different social media profiles available. E.g. one will have only Instagram, but another one will only have their website..etc.
Thanks,
Kevin
OK, you should be able to do this with 1 x Block Element, and as you're running Yoast you should have the additional user profile fields, which can be added to your GB Buttons ( or Headline ) Blocks. See here:
Hi David,
Thank you so much! I didn't know that icons were automatically hidden if there was no link available for the author.
Best,
Kevin
You're welcome!