wordpress 不仅是博客,很多时候 wordpress 还被用作为 CMS (内容管理系统). 博主们喜欢为每个文章加上统一大小的缩略图,尤其是信息类平台. 其中比较常用的处理办法是用 custom field 向文章插入图片,通过上传大小一致的小图或者使用 PHPThumb 等工具生成缩略图.
![](https://files.jb51.cc/file_images/article/201212/20121225150232068.png)
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" />
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。