Created
September 21, 2015 17:03
-
-
Save rinatkhaziev/53269a542425b1c816fa to your computer and use it in GitHub Desktop.
Display placeholder image when there's no thumbnail for the post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Filter to put a placeholder in, when there's no thumbnail set for post (which is the case for a lot of legacy posts) | |
*/ | |
add_filter( 'post_thumbnail_html', function( $html, $post_id, $post_thumbnail_id, $size, $attr ) { | |
// We have a thumb, return it | |
if ( $html ) | |
return $html; | |
$placeholder_url = 'http://full.url.to/placeholder_image'; | |
$attr = wp_parse_args( $attr, [ 'class' => '' ] ); | |
return sprintf( '<img src="%s" class="%s" title="%s">', | |
esc_url( $placeholder_url ), | |
esc_attr( $attr[ 'class' ] ), | |
esc_attr( get_the_title( $post_id ) ) ); | |
}, 10, 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment