随机缩略图

2、将以下代码添加到主题目录下,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);
}
}else {
set_post_thumbnail($post->ID, (rand(849,872))); //媒体库文件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');
add_action('future_to_publish', 'autoset_featured');

3、在代码第13行,输入媒体库图片文件ID,用英文逗号分开即可。

0

评论0

没有账号?注册  忘记密码?