There’s a plugin that sounds like it does what you need:
https://ww.wp.xz.cn/plugins/wp-auto-featured-image/
Thread Starter
powerj
(@powerj)
Thanks Alex. I have tried to use this plugin but it seems to have some bugs in the latest version. Is there an alternative?
Thread Starter
powerj
(@powerj)
I’ve come up with this, but its obviously not quite right – not sure also at what point it applies the image
add_filter('post_thumbnail_html', 'wt_modify_post_thumbnail_html', 99, 5);
function wt_modify_post_thumbnail_html() {
if ( has_post_thumbnail() && get_post_type() == 'testimonials' ) {
the_post_thumbnail();
} else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/transparent.png" />';
}
}
Hello powerj,
The filter applies the image when there was no featured image is applied post in post type=”testimonials”.
Thanks
filter should return the results, not echo it;
https://codex.ww.wp.xz.cn/Plugin_API
try to modifiy your code accordingly:
add_filter('post_thumbnail_html', 'wt_modify_post_thumbnail_html');
function wt_modify_post_thumbnail_html( $html ) {
if ( !$html && get_post_type() == 'testimonials' ) $html = '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/transparent.png" />';
return $html;
}
related http://justintadlock.com/archives/2012/07/05/how-to-define-a-default-post-thumbnail