- This topic has 22 replies, 2 voices, and was last updated 7 years, 9 months ago by
Tom.
-
AuthorPosts
-
February 25, 2018 at 7:52 am #505211
Dan
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
DanFebruary 25, 2018 at 11:49 am #505358Tom
Lead DeveloperLead DeveloperHi 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 🙂
February 25, 2018 at 6:15 pm #505555Dan
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; }February 25, 2018 at 6:58 pm #505575Dan
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-thumbnailDan
February 25, 2018 at 10:55 pm #505642Tom
Lead DeveloperLead DeveloperTry putting your $default_img variable inside the actual function.
February 26, 2018 at 6:08 am #505866Dan
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; }February 26, 2018 at 10:14 am #506141Tom
Lead DeveloperLead DeveloperLink to the page where a default image should show?
February 26, 2018 at 10:16 am #506144Dan
For example, this archive page
http://qilates.s308.upress.link/category/natural-medicine/Thanks
DanFebruary 26, 2018 at 8:50 pm #506530Tom
Lead DeveloperLead DeveloperTry 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; }February 27, 2018 at 6:40 am #506874Dan
Sorry, still no go
February 27, 2018 at 9:09 pm #507505Tom
Lead DeveloperLead DeveloperLet’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; }March 1, 2018 at 7:03 am #508938Dan
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
March 1, 2018 at 9:10 pm #509377Tom
Lead DeveloperLead DeveloperI just tested this plugin, and it works perfectly: https://wordpress.org/plugins/default-featured-image/
Might be worth a shot.
March 3, 2018 at 11:18 am #510741Dan
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
DanMarch 3, 2018 at 11:22 am #510743Dan
This plugin also has some filters:
https://wordpress.org/plugins/default-featured-image/#faqJust curious why the code itself isn’t working with GP
Dan
-
AuthorPosts
- You must be logged in to reply to this topic.