Archived topic
Default / Fallback featured image
22 replies · Started by Dan on February 25, 2018
Hi
Where can a default/fallback featured image be set up in case the content editor does not upload a featured image for the post?
Thanks
Dan
Hi Dan,
Something like this should work:
add_filter( 'post_thumbnail_html', 'tu_post_thumbnail_fallback', 20, 5 );
function tu_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
if ( empty( $html ) ) {
echo '<img src="URL TO YOUR FALLBACK IMAGE" alt="" />';
}
return $html;
}
Let me know :)
Hi Tom,
Thanks for your reply.
Unfortunately, it doesn't display anything.
I tried both options (hardcoding the image and also via ACF):
$default_img = get_field('default_img', 'options');
add_filter( 'post_thumbnail_html', 'tu_post_thumbnail_fallback', 20, 5 );
function tu_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
if ( empty( $html ) ) {
// echo '<img src=' .$default_img .' alt="Pilatis" />';
echo '<img src="http://localhost/qilates/wp-content/uploads/2018/02/default_thumbnail.jpg" alt="Pilatis" />';
}
return $html;
}
Does GP do a conditional check 'has_thumbnail'?
Maybe that's the reason it's not working?
See "Notes about this method:" section in the below post:
http://justintadlock.com/archives/2012/07/05/how-to-define-a-default-post-thumbnail
Dan
Try putting your $default_img variable inside the actual function.
Thanks Tom,
I removed it and hard coded the image for the purpose of the check, but it's still not showing.
/**default thumbnail for posts**/
add_filter( 'post_thumbnail_html', 'tu_post_thumbnail_fallback', 20, 5 );
//$default_img = get_field('default_img', 'options');
function tu_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
if (empty($html) ) {
// echo '<img src=' .$default_img .' alt="Pilatis" />';
echo '<img src="http://localhost/qilates/wp-content/uploads/2018/02/default_thumbnail.jpg" alt="Pilatis" />';
// echo '<img src="URL TO YOUR FALLBACK IMAGE" alt="" />';
}
return $html;
}
Link to the page where a default image should show?
For example, this archive page
http://qilates.s308.upress.link/category/natural-medicine/
Thanks
Dan
Try this:
add_filter( 'post_thumbnail_html', 'tu_post_thumbnail_fallback', 20, 5 );
function tu_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
if ( empty( $html ) ) {
$html = '<img src="URL TO YOUR FALLBACK IMAGE" alt="" />';
}
return $html;
}
Sorry, still no go
Let's do this instead:
add_filter( 'get_post_metadata', 'tu_fallback_featured_image', 10, 4 );
function tu_fallback_featured_image( $value, $post_id, $meta_key, $single ) {
if ( '_thumbnail_id' === $meta_key ) {
if ( ! $value ) {
$value = 1079; // ID of the fallback image.
}
}
return $value;
}
Hi Tom
This update just overrides all featured images and adds the default image to all the posts.
It even replaces them in the featured image box.
Also, it added the featured image on the top part of the homepage.
Dan
I just tested this plugin, and it works perfectly: https://wordpress.org/plugins/default-featured-image/
Might be worth a shot.
Thanks Tom, this plugin solves the issue, but I prefer solving it without a plugin so I have more control over the featured image, for specific categories, post types etc...
It's not super urgent but would like to know, for future project's sake, if this can be done via code as our previous code efforts.
Thanks
Dan
This plugin also has some filters:
https://wordpress.org/plugins/default-featured-image/#faq
Just curious why the code itself isn't working with GP
Dan