DUX主题随机缩略图及调用第一张图片为缩略图

4,529次阅读
没有评论

共计 1024 个字符,预计需要花费 3 分钟才能阅读完成。

最近用了 DUX1.3 主题优化版,发现内容都没有缩略图了,经查要一个个的指定特色图像才会有。但我的大部分文章都是没有图片的,所以只就能想搞个随机缩略图得了。
1. 随机缩略图
编辑主题 /inc/fn.php 文件,找到
$html = sprintf('DUX 主题随机缩略图及调用第一张图片为缩略图 ', get_stylesheet_directory_uri() .'/img/thumbnail.png', $class);
修改为
$random = mt_rand(1, 15);
$html = sprintf('DUX 主题随机缩略图及调用第一张图片为缩略图 ', get_stylesheet_directory_uri() .'/img/random/'.$random.'.jpg', $class);

然后在主题 /img 目录下新建一个目录 random, 放入 15 张图片,分别命名为 1.jpg 至 15.jpg.

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);
}
}
}
} //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');

正文完
 
Winter
版权声明:本站原创文章,由 Winter 2016-03-25发表,共计1024字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码