Archived topic
How to Add Element to All Posts / Pages by Author
7 replies · Started by Adrien on June 3, 2022
Hello guys,
Before writing this support ticket, I've checked this resources and couldn't achieve the results I need:
- https://generatepress.com/forums/topic/author-box-11/#post-1947236
- https://generatepress.com/forums/topic/add-element-to-posts-by-specific-author/
- https://generatepress.com/forums/topic/add-element-to-posts-by-specific-author-2/
So basically, what I've done and what I'm trying to do:
1. I'd like to create 4 customized author boxes. I tried to do it with dynamic content, but this solution doesn't allow me to be as customized as I want.
2. So, I've created 4 Elements as follow:
-> New Element
-> Select type: Block
-> Settings, element type: hook,
-> Hook name: after_content
If I choose to display the block using the GP display rules, it works without any issue.
3. But I want to show the element on every page & post written by a specific author.
I could select them manually with GP display rules, but as we have lots of content, I'd prefer to do it with PHP snippets.
I've tried the snippets and different versions of it that were shared on previous Support Topics. Whether it didn't work at all, whether it broke the website.
This snippet didn't work, I think it's because of: generate_hook_element_display.
add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
$author_id = get_post_field( 'post_author', get_the_ID() );
if ( 1234 === $element_id && '123' == $author_id ) {
$display = true; // If Element ID is 1234 and author ID is 123, display the Element.
}
if ( 5678 === $element_id && '456' == $author_id ) {
$display = true; // If Element ID is 5678 and author ID is 456, display the Element.
}
return $display;
} );
So I edited the snippet with: generate_block_element_display. And it broke the website.
add_filter( 'generate_block_element_display', function( $display, $element_id ) {
$author_id = get_post_field( 'post_author', get_the_ID() );
if ( 1234 === $element_id && '123' == $author_id ) {
$display = true; // If Element ID is 1234 and author ID is 123, display the Element.
}
if ( 5678 === $element_id && '456' == $author_id ) {
$display = true; // If Element ID is 5678 and author ID is 456, display the Element.
}
return $display;
} );
Finally, I also tried this. And it just didn't work:
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 );
Of course, for each version of the snippets I've tried them by changing the IDs numbers.
What am I doing wrong?
I'll provide you with access to a staging, so that you can test it out.
Thanks a lot,
Adrien
Hi there,
Try this snippet:
https://docs.generatepress.com/article/generate_element_display/
add_filter( 'generate_element_display', function( $display, $element_id ) {
global $post;
$author = get_post_field( 'post_author', get_the_ID() );
if ( 21598 === $element_id && '54' === $author ) {
$display = true;
}
return $display;
}, 10, 2 );
Thank you sooooo much Ying.
GeneratePress support team is definitely the best one on earth!!
Quick question:
I want to do it for 4 authors on the website.
If I want to add the same rule for other Author Bios. Do I create duplicates of this snippet? Or is there a better way to do it, with one snippet containing all the settings?
Thanks again!!
Adrien
So create 4 different block element and get all the element ID and author ID.
Modify the code to something like this:
add_filter( 'generate_element_display', function( $display, $element_id ) {
global $post;
$author = get_post_field( 'post_author', get_the_ID() );
if ( 21598 === $element_id && '54' === $author || 21598 === $element_id && '54' === $author || 21598 === $element_id && '54' === $author || 21598 === $element_id && '54' === $author) {
$display = true;
}
return $display;
}, 10, 2 );
Just simply replace the element ID and author ID :)
Hello! I have implemented this same snippet to display a different page hero for each author. However, it is also displayed on the author archive pages as a hero too. I tried excluding author archive pages on the element itself but that didn't work. Is there a modification that will display it on posts only?
THANKS
Scott
Hi there,
you can change the conditional check eg.
if ( 21598 === $element_id && '54' === $author ) {
To apply to only singular templates such as posts and pages:
if ( 21598 === $element_id && '54' === $author && is_singular() ) {
Worked perfectly - Awesome support in record time! THANKS!
You're welcome