You can try adding this to the functions.php file in your theme. What this does is check if you have a featured image (giving you the ability to still set a custom image if you wish), then if it doesn’t find one it’ll show your avatar. Change 96 to whatever size you want to use.
function avatar_or_featured_image($html, $post_id){
if(has_post_thumbnail($post_id)){
return $html;
} else {
return get_wp_user_avatar(get_the_author_meta('ID'), 96);
}
}
add_filter('post_thumbnail_html', 'avatar_or_featured_image', 10, 2);
If it’s not showing up, check in your template if the featured image is in a conditional like this:
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
If it is, change it so it’s only:
<?php the_post_thumbnail(); ?>
Thank you very much Bangbay! I really appreciate your help with this βΒ truly going above & beyond!
I placed your code in my functions.php file; no result ensued; searched through my theme’s php files for the conditional; found just one instance, which I changed, but it resulted only in the avatar appearing in the Featured Image column of my (back-end) All Posts listing page . . . but not on the individual Edit Post page, and not grabbed for any front-end display.
I’ve obviously missed another instance of that conditional . . . and presumably different themes put it in different locations (I’m using Pagelines framework) β but any ideas where I might find it?
Thank you in advance! This is really into ‘customization’ territory, I know β Though it really ought to be a build-in WP feature/option!
I took a look at the Pagelines theme. In includes/class.posts.php there’s a function called pagelines_show_thumb. Comment out or delete the conditional wrapper and it should work. It’s these two lines:
if( function_exists('the_post_thumbnail') && has_post_thumbnail($post) ){
and
} else return false;
Keep in mind that if you update this theme in the future that your modifications will be overwritten.
That works!!!
Actually, it strangely resulted also in a couple of duplicate images being placed up in the copy-pad div, but those were easily erased with CSS, so no biggie π
Thank you very much!!! A+++++++ developer support! I really do appreciate your help! (And thanks for that over-writing warning, too.)
Gotta learn me some php one of these days…
Hello,
I have added the following code to functions.php
function avatar_or_featured_image($html, $post_id){
if(has_post_thumbnail($post_id)){
return $html;
} else {
return get_wp_user_avatar(get_the_author_meta('ID'), 96);
}
}
add_filter('post_thumbnail_html', 'avatar_or_featured_image', 10, 2);
And I have removed the following conditional tags from loop.php:
<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) : ?>
<?php endif; ?>
I’m unable to get the above code to have the user avatar as the post thumbnail. I’m using “Sahifa” theme by Themeforest. Please help!
@aakarshnarula – Please start a new topic.