admin管理员组文章数量:1325674
I want to use a default image as a featured image for all those Posts and Pages, where is not any selected featured image from media. I have tried this code in function.php but not working,
function filter_post_thumbnail( $html ) {
if ( '' == $html ) {
return '<img src=".png" />';
}
// Else, return the post thumbnail
return $html;
}
add_filter( 'post_thumbnail_html', 'filter_post_thumbnail' );
Have any solution by implementing some piece of code in function.php file?
I want to use a default image as a featured image for all those Posts and Pages, where is not any selected featured image from media. I have tried this code in function.php but not working,
function filter_post_thumbnail( $html ) {
if ( '' == $html ) {
return '<img src="https://upload.wikimedia/wikipedia/commons/f/f9/Google_Lens_-_new_logo.png" />';
}
// Else, return the post thumbnail
return $html;
}
add_filter( 'post_thumbnail_html', 'filter_post_thumbnail' );
Have any solution by implementing some piece of code in function.php file?
Share Improve this question edited Aug 30, 2020 at 2:22 Rup 4,4004 gold badges29 silver badges29 bronze badges asked Aug 29, 2020 at 18:31 PuneetPuneet 477 bronze badges 1- Did Rups response answer your question? I see you've not left any comments but you also haven't upvoted or accepted the answer – Tom J Nowell ♦ Commented Sep 29, 2020 at 11:06
1 Answer
Reset to default 0To make your existing code work you'll also need to override has_post_thumbnail
:
add_filter( 'has_post_thumbnail', '__return_true' );
since your theme is probably guarding get_the_post_thumbnail() with has_post_thumbnail(), so that now needs to return true in the default case.
However there's still a chance that this won't work, if the theme you're using goes directly to get_post_thumbnail_id()
. To make that work, you'll need to have an ID for your image: either
upload the image to the media library
pick an out-of-range ID, e.g. -123, to be the thumbnail attachment ID and then fake the image by hooking
image_downsize
(I think!)function image_downsize_default_thumbnail( $downsize, $id, $size ) { if ( $id == -123 ) { return array( 'https://upload.wikimedia/wikipedia/commons/f/f9/Google_Lens_-_new_logo.png', 1024, 1024, false ); } return $downsize; }
and then add a default_post_metadata
hook to fill in the missing thumbnail ID when absent, which is the postmeta key _thumbnail_id
:
function default_post_metadata__thumbnail_id( $value, $object_id, $meta_key,
$single, $meta_type )
{
if ( '_thumbnail_id' == $meta_key )
{
$value = -123; // the attachment ID for the default image
}
return $value;
}
add_filter( 'default_post_metadata', 'default_post_metadata__thumbnail_id', 10, 5 );
(We can ignore $single as get_metadata_default()
will wrap the value in an array if necessary, although get_post_thumbnail_id()
passes $single = true anyway.)
With this filter you shouldn't need to hook has_post_thumbnail and post_thumbnail_html, as it should pick up the default image as if it were the real thumbnail. You could look up the default ID from a post or from an option here too if you'd prefer rather than hard-coding it.
本文标签: post thumbnailsHow to use default image as featured image by implement some code in functionphp
版权声明:本文标题:post thumbnails - How to use default image as featured image by implement some code in function.php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742148798a2422913.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论