Home › Forums › Pro Support › How to set a default fallback image if a post doesn’t include any image?
Tagged: fall back featured image
- This topic has 7 replies, 2 voices, and was last updated 2 years, 8 months ago by
elvin.
-
AuthorPosts
-
January 19, 2021 at 8:43 am #25681
Maria Rowena
ParticipantHello Support Team,
I use carousel to display custom post in my website. Sometimes, there are post that don’t have featured image or any image at all. Is there a way that I can set a default fall back image if no featured image is set?
As you know, the carousel doesn’t look good if a certain custom post doesn’t have image to display. Hope you can help me here. Thank you so much. π
January 19, 2021 at 7:45 pm #25697elvin
ModeratorHi,
Is the fallback image a static image? If so, you can try this snippet:
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; }
where “URL TO YOUR FALLBACK IMAGE” is the path to your fallback image file.
January 20, 2021 at 12:05 am #25708Maria Rowena
ParticipantThank you for this code Elvin!
I tried this one but it’s not working. I use carousel slider.
See this screen shot. https://snipboard.io/v2QeZN.jpg.
And below is the code we use. π
`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 ‘‘;
}return $html;
}January 20, 2021 at 1:21 am #25710elvin
ModeratorYou can use this plugin instead.
https://wordpress.org/plugins/default-featured-image/#faqJanuary 20, 2021 at 10:43 am #25719Maria Rowena
ParticipantThank you for the suggestion Elvin, I use that plugin before but this time it is the custom post type that does not use the default featured image field of WordPress.
Is there a way that we can set a default fall back image for the wp-show post list or carousel?
Does the code you gave to me won’t work anymore?
Looking forward for a positive response. Thank you so much! π
January 20, 2021 at 4:33 pm #25738elvin
ModeratorDoes the code you gave to me wonβt work anymore?
It doesn’t work because of this line of code within the theme.
https://github.com/tomusborne/wp-show-posts/blob/35e410d7800273fc66f211c0f80d553e95d17f83/inc/functions.php#L154Commenting out this whole
if ( ! has_post_thumbnail() ) {return;}
will make my code work but that means you’ll have to edit the plugin’s code. While editing the plugin’s code can work, the issue is you’ll have to do this again once the plugin updates because plugin updates wipe any code changes you’ve made prior to it.You can try their code here to make the plugin work on custom post types.
https://wordpress.org/support/topic/create-default-featured-image-for-one-custom-post-type-only/#post-13892814January 21, 2021 at 7:25 pm #25804Maria Rowena
ParticipantThank you so much! It works! π
January 21, 2021 at 11:23 pm #25811elvin
ModeratorNice one. Glad it works for you. π
-
AuthorPosts
- You must be logged in to reply to this topic.