Add a Default Featured Image for all posts & pages.
Add a default featured image to the media settings page. This featured image will show up if no featured image is set. Simple as that.
Take a look at FAQ for the basic questions.
Feel free to contact me on the forum or on the github repository.
/wp-content/plugins/
directoryThis plugin works out of the box for most cases, but not always. If it doesn’t work you can try the following things.
default-featured-img
class added to them.Still having problems? I want to know if it fails, so contact me
There are no new functions, all core WordPress functions can be used.
Yes, the following code will set a different image.
add_filter( 'dfi_thumbnail_id', 'dfi_posttype_book', 10, 2 ); function dfi_posttype_book( $dfi_id, $post_id ) { $post = get_post( $post_id ); if ( 'book' === $post->post_type ) { return 31; // the image id for the book post type. } return $dfi_id; // the original featured image id. }
Yes, the following snippet will set different images based on the category.
add_filter( 'dfi_thumbnail_id', 'dfi_category', 10, 2 ); function dfi_category( $dfi_id, $post_id ) { // Set a different image for posts that have the 'cats' category set. // This will trigger first, if multiple categories have been set. if ( has_category( 'cats', $post_id ) ) { return 7; // cats img id. } // Set a different image for posts that have the 'cats' category set. if ( has_category( 'dogs', $post_id ) ) { return 8; // dogs img id. } return $dfi_id; // the original featured image id. }
When a Default Featured Image is used it will already add an extra class default-featured-img
.
This can be used for styling.
If you need more you can change the whole HTML with the filter dfi_thumbnail_html
.
add_filter( 'dfi_thumbnail_html', 'dfi_add_class', 10, 5 ); function dfi_add_class( $html, $post_id, $default_thumbnail_id, $size, $attr ) { // Add a class to the existing class list. $attr['class'] .= ' my-class'; return wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr ); }
The following code will exclude the post/page with ID 23.
add_filter( 'dfi_thumbnail_id', 'dfi_skip_page', 10, 2 ); function dfi_skip_page( $dfi_id, $post_id ) { if ( $post_id == 23 ) { return 0; // invalid id. } return $dfi_id; // the original featured image id. }
get_post_meta($post_id)
without specifying the meta_key didn’t find the DFI. It will now even use an even deeper level and set it in the core cache.dfi_thumbnail_id
now also returns the post ID of the post (or any postype) that is being called. See the FAQ for new examplesdfi_thumbnail_id
is now called in an earlier stage.<img>
tag, if it shows a default featured imageget_post_thumbnail_id
, making the chance that it fail far far smaller.