最近遇到很多朋友在说,把图片存到云端,从而提升博客的速度,但是很多人不知道特色图片怎么外链,其实wordpress的特色图片是不能使用外链地址的。
所以我们就只能使用今天说到的这个方法,调用文章的第一张图片作为特色图片,这样咱们就可以把图片存到云端了。
那我们怎么才可以获得文章中的第一张图片,并把它显示在网页中呢?
首先,打开你主题的Function.php文件,在其中加入以下代码:
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 $first_img = "/images/default.jpg"; } return $first_img; }
第二步,在你的模板的主循环中加入以下代码:
<?php echo catch_that_image() ?>
这样就可以把文章中的第一张图片显示在页面上了...