Dux主题首页缩略图不显示bug修复,说是bug修复,其实也不是,因为主题购买页已经说得很清楚了:
为什么没有缩略图? 你需要编辑文章的时候添加特色图片,这样才会显示在列表的缩略图中;
也就是说不设置特色图片,首页就显示默认的图片。这在主题说明里面已经详细说明啦,亲们,你们不要纠结了,dux主题如何设置文章第一张图片为首页所缩略图呢?
有的朋友会想到增加一个函数,也就是自动提取第一张图片为首页缩略图的函数,也有的朋友想到在不改函数名的情况下替换dux主题的缩略图函数,这两种方法都可行,但都有缺点,缺点就是比较麻烦,需要修改主题文件中所有调用该缩略图函数的地方。
自动提取第一张图片为首页缩略图的函数
/* 抓取文章第一张图片作为特色图片(已加上是否已存在判断,可放心添加到functions.php) */
if(!function_exists('catch_first_image')){
function catch_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content,$matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
$random = mt_rand(1, 10);
$first_img='https://dedewp.com/wp-content/themes/xiu2.1/images/random/'.$random.'.jpg';
}
return $first_img;
}
}
那么还有其他办法么?

请输入验证码查看内容
扫码回复关键字“92wp”获取验证码
再分享下如何实现Dux主题文章不设置特色图片或者文章使用外链图片时显示随机缩略图?
function _get_post_thumbnail($size = 'thumbnail', $class = 'thumb') {
$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 {
$random = mt_rand(1, 10);
$html = sprintf('<img data-src="%s" class="%s">', get_stylesheet_directory_uri() . '/img/random/'.$random.'.jpg', $class);
}
return $html;
}
覆盖同名函数,然后添加图片.jpg到img/random即可即可



