### [wordpress怎么使用外链图片作为文章缩略图](https://www.prosaas.cn/article/9826) **Published:** 2019-11-07T02:44:11 **Author:** 5201314 **Excerpt:** 思路:1、要有一个确定图片地址的方法:文章中的第一张图片,或者使用自定义栏目增加一个自定义值。2、在前台调用确… ![](//zhiqipro.oss-cn-beijing.aliyuncs.com/2023/12/20231212132812218.jpg) **思路:** 1、要有一个确定图片地址的方法:文章中的第一张图片,或者使用自定义栏目增加一个自定义值。 2、在前台调用确定好的图片:采用函数的方法还是直接调用图片。 **实现如下:** **前提:** 任何调用最好都是在LOOP循环中,这样可以轻松的使用$post值。 1、调用文章中的第一张图片:使用$post->post\_content获得文章内容,然后用匹配的方法得到第一张图片的src值。 preg\_match('//i',$post->post\_content,$index\_piclink); if(count($index\_piclink) >= 2)$image\_src = $index\_piclink\[1\]; if(!strstr($image\_src,'http://'))$image\_src = false; 2、调用一个自定义栏目:在写文章的时候,增加一个名词为post\_thumb的自定义栏目,然后将图片的地址作为值建立它。如`meta_key:post_thumb,meta_value:http://www.utubon.com/images/logo.png`,然后通过以下的方法调用它: $image\_src = get\_post\_meta($post->ID,'post\_thumb',true); $image\_src = trim($image\_src) !== '' ? trim($image\_src) : false; 3、在文章循环中使用它们 if($image\_src)echo ''; 4、把他们做成函数 function get\_thumb\_src($size = 'thumbnail',$first\_pic\_in\_ctonte = true){ global $post; $image\_src = ''; if(function\_exists('has\_post\_thumbnail') && has\_post\_thumbnail()){ $image\_id = get\_post\_thumbnail\_id(); $image\_src = wp\_get\_attachment\_image\_src($image\_id,$size); $image\_src = $image\_src\[0\]; }else{ $image\_src = get\_post\_meta($post->ID,'post\_thumb',$single=true); if(!$image\_src && $first\_pic\_in\_ctonte){ preg\_match('//i',$post->post\_content,$index\_piclink); if(count($index\_piclink) >= 2)$image\_src = $index\_piclink\[1\]; if(!strstr($image\_src,'http://'))$image\_src =false; } } return $image\_src; } function the\_thumb\_src($size = 'thumbnail',$first\_pic\_in\_ctonte = true){ echo get\_thumb\_src($size,$first\_pic\_in\_ctonte); } 这个函数(把它放在functions.php中)实现了对文章缩略图的挑选,如果已经有特色图片,则使用特色图片,如果没有就检查post\_thumb自定义栏目,如果也没有就使用文章第一张图片,如果文章没有图片,就返回false值。在使用时如下: if(get\_thumb\_src())the\_thumb\_src(); 推荐教程:wordpress教程 以上就是wordpress怎么使用外链图片作为文章缩略图的详细内容,更多请关注知企PROSAAS其它相关文章! **Categories:** WordPress ---