坏处是直接使用文章中的原图而非生成压缩缩略图会导致页面载入数据量变大,建议配合lazyload使用。
打开functions-theme.php找到如下代码:
function _get_post_thumbnail() { return ’<img src="’._the_theme_thumb().’" data-src="’. _get_post_thumbnail_url() .’" class="thumb">’; }
替换为如下代码
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; }