使用七牛的同学会知道,即使使用了七牛,可是图片缩略图还是以前的,显示不了七牛的图片。虽然说七牛也有可以调整图片大小的API,可是对于一般的同学来说还是挺麻烦的,而且也是大材小用。现在分享一个不使用API让所有wordpress都支持外链缩略图(可调整大小)
这是这个的功能:
用 WordPress 提供的自定义栏目功能来解决。
$postImageUrl = get_post_meta($post->ID, 'thumbnail', true); //设置自定义栏目名为“thumbnail”
1.在functions.php文件中添加下面代码:
//缩略图获取 add_theme_support( 'post-thumbnails' ); set_post_thumbnail_size( 262, 114 ,true );//设置缩略图的尺寸 function dm_the_thumbnail() { global $post; $postImageUrl = get_post_meta($post->ID, 'thumbnail', true); //设置自定义栏目名为“thumbnail” // 判断该文章是否设置有缩略图,如果有则直接显示 if ( has_post_thumbnail() ) { echo '<span class="work-thumbnail">'; the_post_thumbnail(); echo '</span>'; } elseif ($postImageUrl) { echo '<span class="work-thumbnail"><img alt="" src="'.$postImageUrl.'" /></span>'; } else { //如果文章没有设置缩略图,则查找文章内是否包含图片 $content = $post->post_content; preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER); $n = count($strResult[1]); if($n > 0){ // 如果文章内包含有图片,就用第一张图片做为缩略图 echo '<span class="work-thumbnail"><img alt="" src="'.$strResult[1][0].'" /></span>'; }else { // 如果文章内没有图片,则用默认的图片 echo '<span class="work-thumbnail"><img alt="" src="'.get_bloginfo('template_url').'/images/thumbnail.jpg" /></span>'; } } }
2.折腾完这些步骤以后,需要后台界面右上角点击显示选项,勾选自定义栏目。
3.获取timthumb.php文件,放在主题根目录下
然后调整样式之类的,关于代码中的结构和类名可以修改为适合自己所使用的主题。
83142 228381This constantly amazes me exactly how blog owners for example yourself can discover the time and also the commitment to maintain on composing wonderful blog posts. Your site isexcellent and one of my own ought to read blogs. I basically want to thank you. 117559
2018年8月17日 00:49收下了 试试新版本中是否有效
2016年8月10日 11:52好东西,我的缩略图就显示不了了,收藏先,回家弄弄去。
2014年10月5日 08:14我看你的缩略图好好的啊
2014年10月5日 09:30不好啊,全是系统自带的
2014年10月6日 08:07相关文章里的?设置一下特殊图像应该就好了吧
2014年10月6日 13:08原来是可以的,现在不知道出了什么问题
2014年10月6日 15:57什么问题啊
2014年10月6日 16:10我也不知道,没找到原因
2014年10月6日 16:19是不是你的主题原本就不带最新文章显示缩略图啊
2014年10月6日 16:21带,以前是好的
2014年10月6日 16:34前來支持一下~不過我還是喜歡自己上傳圖片 XDD
2014年10月2日 23:29自己上传就一点事没有了
2014年10月3日 13:41