思绪:
1、要有一个肯定图片地点的要领:文章中的第一张图片,或许运用自定义栏目增添一个自定义值。
2、在前台挪用肯定好的图片:采纳函数的要领照样直接挪用图片。
完成以下:
条件:
任何挪用最好都是在LOOP轮回中,如许能够轻松的运用$post值。
1、挪用文章中的第一张图片:运用$post->post_content取得文章内容,然后用婚配的要领获得第一张图片的src值。
preg_match('/<img.+src=[\'\"]([^\'\"]+)[\'\"].* \/>/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 '<img src="'.$image_src.'" />';
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('/<img.+src=[\'\"]([^\'\"]+)[\'\"].* \/>/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怎样运用外链图片作为文章缩略图的细致内容,更多请关注ki4网别的相干文章!