13
2018
10

WordPress外链的图片做缩略图方法

缩略图一直以来都比较重要,有了图片才能吸引读者,读者才能更好的阅读,但是,WordPress默认的特色图片是只能上传到服务器后才能当做缩略图的,我们想要用外链当缩略图怎么办?

没事,本文教你,这是一个WordPress更好的缩略图方法。

functions.php加入:

/* WordPress更好的缩略图方法 https://www.qiquanji.com/ */
function post_img(){
	global $post;
	if( $values = get_post_custom_values("post_img") ) {
		$values = get_post_custom_values("post_img");
		$post_thumbnail_src = $values [0];
	} elseif( has_post_thumbnail() ){
		$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
		$post_thumbnail_src = $thumbnail_src [0];
	} else {
		$post_thumbnail_src = '';
		ob_start();
		ob_end_clean();
		$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
		$post_thumbnail_src = $matches [1] [0];
		if(empty($post_thumbnail_src)){
			$post_thumbnail_src = get_bloginfo('template_url')."/img/no-img.png";
		}
	};
	echo $post_thumbnail_src;
}

保存后,我们要在主题根目录建一个img文件夹,并且放入一张图片,重命名为no-img.png,这用来没有特色图的时候尴尬场面,调用一张图片。

完成后,我们调用缩略图代码的时候就可以用:

<?php echo post_img();?>

原文链接:https://www.qiquanji.com/post/7357.html

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

微信扫码关注

更新实时通知

« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。