admin管理员组文章数量:1425913
Lot of subject talk about it but seem to be outdated.
I use the new version of Wordpress (5.2.2), and no plugin or code snippet working.
Someone already has this problem ?
I already use plugin the Auto Post Thumbnail, and it's not working. Do you know a plugin for that ?
Lot of subject talk about it but seem to be outdated.
I use the new version of Wordpress (5.2.2), and no plugin or code snippet working.
Someone already has this problem ?
I already use plugin the Auto Post Thumbnail, and it's not working. Do you know a plugin for that ?
Share Improve this question edited Jul 1, 2019 at 13:12 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Jul 1, 2019 at 10:22 Pierre MétéyéPierre Météyé 11 Answer
Reset to default 0Please try the code given below:
function auto_featured_image() {
global $post;
if (!has_post_thumbnail($post->ID)) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
}
// Use it temporary to generate all featured images
add_action('the_post', 'auto_featured_image');
// Used for new posts
add_action('save_post', 'auto_featured_image');
add_action('draft_to_publish', 'auto_featured_image');
add_action('new_to_publish', 'auto_featured_image');
add_action('pending_to_publish', 'auto_featured_image');
add_action('future_to_publish', 'auto_featured_image');
I have tested this code in WordPress 5.2.2
本文标签: How to automatically add first image of post as thumbnail
版权声明:本文标题:How to automatically add first image of post as thumbnail? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745353927a2654938.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论