^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 腾讯云3年2核2G新品轻量限时特惠只需408元

Dux主题修复首页缩略图不显示bug

Dux主题修复首页缩略图不显示bug

Dux 主题首页缩略图不显示 bug 修复,说是 bug 修复,其实也不是,因为主题购买页已经说得很清楚了:

2 核 2G 限时特惠 396 元/3 年    宝塔建站 10850 大礼包

为什么没有缩略图? 你需要编辑文章的时候添加特色图片,这样才会显示在列表的缩略图中;

也就是说不设置特色图片,首页就显示默认的图片。这在主题说明里面已经详细说明啦,亲们,你们不要纠结了,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;
}

覆盖同名函数,然后添加图片.jpgimg/random即可即可

赠人玫瑰,手有余香。