开发随记 – 自动设置文章第一张图为略缩图

               

By Justbi, 2018年5月22日, 195 views

function wpforce_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’, ‘wpforce_featured’);
add_action(‘save_post’, ‘wpforce_featured’);
add_action(‘draft_to_publish’, ‘wpforce_featured’);
add_action(‘new_to_publish’, ‘wpforce_featured’);
add_action(‘pending_to_publish’, ‘wpforce_featured’);
add_action(‘future_to_publish’, ‘wpforce_featured’);
//自动添加文章第一个图为略缩图

把这段代码加入 functions.php

Loading添加到收藏

What do you think?

发表评论

电子邮件地址不会被公开。