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应用外链图片作为文章缩略图,如此简略,你学会了吗?
哦!对了,除此之外,引申一个常识点,咱们能够应用上面的办法应用the_post_thumbnail函数也能够完成应用外链图片的性能。前提是你依照下面的思绪,写好了一个函数,我的完成办法如下:
1、在functions.php中退出如下代码
function the_post_thumb($thumb,$post_id,$post_image_id){
if($thumb == ''){
$thumb = '<img src="'.get_post_meta($post_id,'post_thumb',true).'" />';
}
return $thumb;
}
add_filter('post_thumbnail_html','the_post_thumb',10,3);
2、在调用图片时应用如下代码(文章LOOP中)
<?php if(has_post_thumbnail() || get_post_meta($post->ID,'post_thumb') != ''): ?><p><?php the_post_thumbnail('post-thumbnail'); ?></p><?php endif; ?>
你能够发现,我只采纳了添加自定义栏目post_thumb的办法,而没有添加文章**张图的性能,这是因为我思考到文章**张图可能不是我想要的图片。
除此之外,咱们甚至还能够应用javascript代码,经过ajax获取图片,再在前台经过修正元素内容的办法完成该性能。
以上就是安达网络工作室关于《wordpress使用外链图片作为文章缩略图的方法》的一些看法。更多内容请查看本栏目更多内容!
wordpress完成随机文章 ralix曾公布过对于wordpress随机文章的相干插件的点评文章(“wordpress插件之...
本文实例讲述了在WordPress治理页面底部自定义文字的办法,分享给大家供大家参考。详细完成办法如下: 把上面...
当然 Wordpress 2.7 里有“小工具”选项也能管制 Widget ,然而重复试验后发现款式无奈对立,可控...
1、登录QQ邮箱,点击右下角“浏览空间”,进入浏览空间。 2、进入浏览空间后,点击页面右上角&ld...
本文实例讲述了WordPress评论中制止HTML代码显示的办法。分享给大家供大家参考。详细剖析如下: 应用WordPr...
comments_template()(获取评论模板) comments_template() 函数用来获取评论模板,普通只能用在文章或许页...