代码如下 使用方法直接加入到主题函数【Functions.php】即可
add_theme_support( 'post-thumbnails' );
function catch_that_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)){ //Defines a default image
$popimg=get_option( 'mao10_popimg');
$first_img = "$popimg";
}
return $first_img;
}
function mmimg($postID) {
$cti = catch_that_image();
$showimg = $cti;
has_post_thumbnail();
if ( has_post_thumbnail() ) {
$thumbnail_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail');
$shareimg = $thumbnail_image_url[0];
} else {
$shareimg = $showimg;
};
return $shareimg;
}
在需要显示的地方调用方法:
<img src="<?php echo mmimg(get_the_ID()); ?>" alt="" />
以上代码来自:https://www.mywpku.com/setup-featured-image-in-wordpress-via-url.html 本博客就是使用了此方法解决了首页加载慢的问题,如果有DOMContentL过慢而且找不到原因可以使用此方法尝试。
我写博客不喜欢加图片,也是懒到爆了
一人一种习惯嘛~