前端之家收集整理的这篇文章主要介绍了
使用WordPress内置图片仓库制作缩略图的小技巧,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
wordpress 不仅是博客,很多时候 wordpress 还被用作为 CMS (内容管理系统). 博主们喜欢为每个文章加上统一大小的缩略图,尤其是信息类平台. 其中比较常用的处理办法是用 custom field 向文章插入图片,通过上传大小一致的小图或者使用 PHPThumb 等工具生成缩略图.

2.7 开始,
wordpress 大幅提升多媒体
功能,越来越多人使用 WP 的内置
图片仓库. 对这些
用户来说,制作
缩略图变得并不那么困难,在
上传图片的时候就会默认
生成 150x150 规格的小图 (如果
图片高度/宽度不足 150px,使用原高度/宽度). 那我们可以充分利用这个
功能,在
文章列表上
加上这个
图片作为
缩略图. 这样处理各有利弊,好处是简单,智能 (不用每次输入
缩略图),坏处是消耗服务器流量. Okay,现在要做的就是
提取上传生成的小
图片,并放置在
文章的适当位置. 我创建了一个
文件 thumb.
PHP,
图片获取和
调用一起处理,
文件内容如下. <div class="msgborder" id="
PHPcode16"> <?php $args = array( 'numberposts' => 1,'order'=> 'ASC','post_mime_type' => 'image','post_parent' => $post->ID,'post_status' => null,'post_type' => 'attachment' ); $attachments = get_children($args); $imageUrl = ''; if($attachments) { $image = array_pop($attachments); $imageSrc = wp_get_attachment_image_src($image->ID,'thumbnail'); $imageUrl = $imageSrc[0]; } else { $imageUrl = get_bloginfo('template_url') . '/img/default.gif'; } ?> <a href="<?php the_permalink() ?>"><img class="left" src="<?php _fcksavedurl=""<?php" _fcksavedurl=""<?php" echo $imageUrl; ?>" alt="<?php the_title(); ?>" width="150" height="150" />