{教程}自动为WP文章设置封面图


用来自动设置第一张图为封面图,参考本站,打开你的主题functions.php文件

function autoset_featured() {

global $post;

$already_has_thumb = has_post_thumbnail($post->ID);

if (!$already_has_thumb)  {

$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);

}

}

}

}  //end function

add_action('the_post', 'autoset_featured');

add_action('save_post', 'autoset_featured');

add_action('draft_to_publish', 'autoset_featured');

add_action('new_to_publish', 'autoset_featured');

add_action('pending_to_publish', 'autoset_featured');

 

版权声明:
作者:月宅
链接:https://xn--09st1ywm8a.com/1790.html
来源:月宅酱
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>