DUX主题在缩略图显示部分设计的不是很灵活,DUX主题默认只支持显示文章设置的特色图像和默认缩略图。即文章设置了特色图像,在浏览时显示特色图像,如果文章未设置特色图像,则只能显示主题默认的图片文件。怎样才能使DUX主题在文章没有设置特色图像的情况下,自动提取第一张图片作为缩略图或者随机显示某张图片作为缩略图呢?
文章源自堕落的鱼-https://www.duoluodeyu.com/1893.html
堕落的鱼通过修改/inc/fn.php文件实现,找到/inc/fn.php文件中的_get_post_thumbnail函数,将其修改为以下内容:文章源自堕落的鱼-https://www.duoluodeyu.com/1893.html
- function _get_post_thumbnail($size = 'thumbnail', $class = 'thumb') {
- global $post;
- $html = '';
- if (has_post_thumbnail()) {
- /*$domsxe = simplexml_load_string(get_the_post_thumbnail());
- $src = $domsxe->attributes()->src;
- $src_array = wp_get_attachment_image_src(_get_attachment_id_from_src($src), $size);
- $html = sprintf('<img data-src="%s" class="%s"/>', $src_array[0], $class);*/
- $domsxe = get_the_post_thumbnail();
- // print_r($domsxe);
- preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $domsxe, $strResult, PREG_PATTERN_ORDER);
- $images = $strResult[1];
- foreach($images as $src){
- $html = sprintf('<img data-src="%s" class="thumb">', $src);
- break;
- }
- }else {
- $post_thumbnail_src = '';
- ob_start();
- ob_end_clean();
- $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
- $post_thumbnail_src = $matches [1] [0]; //获取该图片 src
- if(empty($post_thumbnail_src)){
- $random = mt_rand(1, 10);
- $post_thumbnail_src = get_bloginfo('template_url').'/img/pic/'.$random.'.jpg';
- }
- $html = sprintf('<img data-src="%s" class="thumb">', $post_thumbnail_src);
- }
- return $html;
- }
需在主题目录下的/img/pic/目录下放置10张图片,图片命名格式为1.jpg ..... 10.jpg.文章源自堕落的鱼-https://www.duoluodeyu.com/1893.html
以上文件修改后即可实现如有特色图像,缩略图显示特色图像,无特色图像则用文章内第一个图片作为缩略图,如果文章内无图片,则随机显示图片上步设置的10张随机图片中的一张。文章源自堕落的鱼-https://www.duoluodeyu.com/1893.html
_get_post_thumbnail源码下载文章源自堕落的鱼-https://www.duoluodeyu.com/1893.html 文章源自堕落的鱼-https://www.duoluodeyu.com/1893.html
1F
多谢博主啊,找了很久都没找到,这里找到了
2F
请教下博主!怎么取消缩略图显示之前的预览图?就是未显示的那个图片,直接把特色图像显示出来就行了,未显示的图片加载的时候看着难受
B1
@ 更省APP 是可以去掉的,但是我好久没接触过DUX主题了,所以现在也不知道该怎么修改了。不好意思。