Archived topic
Avatar as the featured image
7 replies · Started by Rogi on November 21, 2020
There is a way to set the post author avatar as the default featured image?
Hi,
I'm not sure I understand what you're trying to do.
Can you explain a bit more?
To clarify: Do you mean, you want the post author avatar as the default fallback image when there's no featured image assigned to a post?
Or do you mean, you want the post author avatar to be the featured image of posts/pages, regardless if there's an image assigned?
Hi Elvin.
I want the post author avatar to be the featured image of posts/pages, regardless if there’s an image assigned.
Thanks.
You can try adding this PHP snippet.
add_filter( 'generate_featured_image_output', function(){
return sprintf(
'<div class="post-image">
%3$s
<a href="%1$s"><img class="author-image" src="%2$s"></a>
</div>',
esc_url( get_permalink() ),
lh_page_hero_gravatar(),
apply_filters( 'generate_inside_featured_image_output', '' )
);
});
function lh_page_hero_gravatar() {
global $post;
$author_id = $post->post_author;
return get_avatar_url( $author_id,['size' => '400'] );
}
Here's how to add PHP - https://docs.generatepress.com/article/adding-php/
You can adjust the size by changing the 400 to a value of your preference.
Elvin,
There was an error with the code.
Please see
Hi there,
in the code Elvin provided you will see two instances of:
lh_page_hero_gravatar
Change both of them to something unique eg.
igor_page_hero_gravatar
Thank you guys.
Thank you guys.
Glad to be of any help. No problem. :)