正在加载...

blog » WordPress / 软硬一本 » 如何使用WordPress 2.9内置的缩略图功能

如何使用WordPress 2.9内置的缩略图功能

恩...话说2.9更新以后,我最关心的其实就是内置缩略图的这个功能,很想马上在鸸鹋动物园上做实验。
对于WordPress的伪CMS化来说,这个功能几乎可以说是最大的进步——当然也是越来越臃肿...

确定,官方的函数已经由the_post_image改为the_post_thumbnail。

但是...在折腾了一个小时以后我也没有在文章编辑的后台发现关于缩略图的模块(英文称作Post Thumbnail):

正常应该是这样...为什么呢?伟大的搜索引擎再次帮助了我,但是我很难理解为什么要这么做,既然已经是内置功能

functions.php中添加:

  1. if ( function_exists( 'add_theme_support' ) ) {
  2. add_theme_support( 'post-thumbnails' );
  3. }

好了,添加以后就可以正常显示这个模块了,至于操作就很简单了没什么可说的。缩略图添加完了,在主题(theme)内如何显示呢?通常我们只会在index.php或者archive.php之类的地方显示缩略图,以默认模版的index.php为例,将如下部分:

  1. <div class="entry">
  2.     <?php the_content('Read the rest of this entry &raquo;'); ?>
  3. </div>

修改为:

  1. <div class="entry">
  2. <?php if ( has_post_thumbnail() ) : ?>
  3.     <?php the_post_thumbnail( array( 125, 125 ), array( 'class' => 'alignleft' ) ); ?>
  4. <?php endif; ?>
  5.     <?php the_content('Read the rest of this entry &raquo;'); ?>
  6. </div>

其中,arry(125, 125)是说缩略图的大小,同样也可以根据后台的设置,指定为thumbnail、medium、large或者full。
至于array( 'class' => 'alignleft' ),指定的就是图片的class效果了。

进一步的,如果这篇文章没有图,那么我们怎么显示一张默认的图片(例子.jpg)呢?

  1. <?php if ( has_post_thumbnail() ) : ?>
  2.     <?php the_post_thumbnail( array( 'thumbnail' ), array( 'class' => 'alignleft' ) ); ?>
  3. <?php else : ?>
  4.     <img src="例子.jpg" />
  5. <?php endif; ?>

恩...大概应该就可以了...我只是奇怪,关于缩略图的函数,在codex.wordpress.org站上统统没有详细说明——实在很费解(起码此时此刻还没有)

the_post_thumbnail (2.9+)
get_the_post_thumbnail (2.9+)
has_post_thumbnail (2.9+)
get_post_thumbnail_id (2.9+)

可是实际上,在鸸鹋动物园上我还是没有这么采用缩略图,因为它毕竟还是要手动的去选择一下缩略图——我不知道对于以往的几千篇文章,应该怎么自动调用这个缩略图,有知道的朋友望提示。
所以呢,我还是在使用布谷鸟自制的这个thumbnails_anywhere插件:)

Popularity: 16%

延展阅读

RSS 2.0 | leave a response | trackback

35 Responses

  1. Dec 21st, 2009 / 12:15 pm Reply to this comment zwwooooo Says:

    我还是不用这个功能了,也好像不太需要

  2. Dec 21st, 2009 / 12:19 pm Reply to this comment XJP Says:

    地板位,好像还不太迟

  3. Dec 21st, 2009 / 12:19 pm Reply to this comment bolo Says:

    暂时观望一下,以前通过自定义字段定义的缩略图就需要进行转换了

  4. Dec 21st, 2009 / 12:32 pm Reply to this comment Dianso Says:

    我还没试过这个功能呢

  5. Dec 21st, 2009 / 4:00 pm Reply to this comment Jinwen Says:

    我还没开始折腾这个呢。我想可以再加一个判断,如果还没有缩略图的话就读取日志中首张图片吧,或者显示一张默认的,临时方法。

  6. Dec 21st, 2009 / 4:21 pm Reply to this comment DIV里面装B Says:

    博主真是SB,那么多404CSS。

  7. Dec 21st, 2009 / 4:22 pm Reply to this comment DIV里面装B Says:

    哈哈,最B4要审核的博客,SB。

  8. Dec 21st, 2009 / 4:22 pm Reply to this comment underone Says:

    @Jinwen: 应该是能想个办法,然后之前的所有日志都自动添加上这个,否则对老网站这个功能岂不是就没意义了:)

  9. Dec 21st, 2009 / 4:29 pm Reply to this comment underone Says:

    @DIV里面装B:
    是哦...因为在中国,所以这个网络上有很多需要装逼的博客
    是哦...我了解的css没你了解的多,所以马化腾雇你没雇我
    但起码我不像你这么没胆量,连个真实ID都不敢用,何必呢?
    干嘛一定把别人仅仅是善意的询问都当成恶意的呢:) 当然了,我宁愿我猜错了

  10. Dec 21st, 2009 / 4:44 pm Reply to this comment joe Says:

    你是怎么知道他是TX的?

  11. Dec 21st, 2009 / 4:50 pm Reply to this comment underone Says:

    @joe: 猜测.

  12. Dec 21st, 2009 / 5:03 pm Reply to this comment bigCat Says:

    - - 同学,淡定,建议去后台评论区查下IP先再猜比较准一点,哈哈
    对于这种脑残留言直接删掉就行了,或mark为akismet里的spam

  13. Dec 21st, 2009 / 5:22 pm Reply to this comment linxz Says:

    underone Says:
    @DIV里面装B:
    是哦...因为在中国,所以这个网络上有很多需要装逼的博客
    是哦...我了解的css没你了解的多,所以马化腾雇你没雇我
    但起码我不像你这么没胆量,连个真实ID都不敢用,何必呢?
    干嘛一定把别人仅仅是善意的询问都当成恶意的呢:) 当然了,我宁愿我猜错了

    ===============
    很希望你是猜错的,或者我们还是不要这样猜测比较妥当。和谐的社会才是最主要的,望博主淡定!:)

  14. Dec 21st, 2009 / 5:29 pm Reply to this comment underone Says:

    @bigCat:
    我很淡定啊,不看IP我也不这么说啊:)
    @linxz:
    我倒是没别的意思,就是觉得那位同学脾气太大了

  15. Dec 21st, 2009 / 6:08 pm Reply to this comment bigCat Says:

    赞,暴露了,咱这边搞css的不会菜到不用跳板的,所以可能是其他部门

  16. Dec 21st, 2009 / 6:54 pm Reply to this comment 万戈 Says:

    等升到2.9后我也要研究这个功能

  17. Dec 21st, 2009 / 9:02 pm Reply to this comment 齐林 Says:

    还要这么麻烦啊

  18. Dec 21st, 2009 / 10:05 pm Reply to this comment A.shun Says:

    有新功能是好事,虽然暂时用不上

  19. Dec 21st, 2009 / 11:49 pm Reply to this comment eddie Says:

    好像新加的函数一般codex没那么快更进的
    顺便鄙视那个捣乱的人

  20. Dec 22nd, 2009 / 1:24 pm Reply to this comment Rogers Says:

    还是观望下。目前看没什么特别需要升级的动力。

  21. Dec 22nd, 2009 / 2:33 pm Reply to this comment Hobo Says:

    新版的WP还是有很多值得研究的东西

  22. Dec 23rd, 2009 / 12:42 am Reply to this comment FORRSS Says:

    学习了。

  23. Dec 25th, 2009 / 10:50 pm Reply to this comment aunsen Says:

    BS上面那个乱叫的人!丸子不用理他!

  24. Dec 29th, 2009 / 12:07 pm Reply to this comment crossyou Says:

    我启用了后 上传图片是 报错了。不知道是怎么回事。

  25. Dec 29th, 2009 / 4:26 pm Reply to this comment sweetwater Says:

    经常来你这里的,呵呵,顶一下

  26. Jan 28th, 2010 / 5:41 pm Reply to this comment weweloo Says:

    很有用的信息,下班回家试试看。

  27. Feb 1st, 2010 / 2:09 pm Reply to this comment 百奥博 Says:

    正在全力研究这个功能。

  28. Feb 2nd, 2010 / 12:23 am Reply to this comment pangel Says:

    刚升级291试试看

  29. Feb 11th, 2010 / 6:57 pm Reply to this comment shapn Says:

    这个 不错 这个功能 我还没用

  30. Feb 24th, 2010 / 4:46 pm Reply to this comment 孙悟空 Says:

    深刻滴学习了。

  31. Mar 3rd, 2010 / 10:11 am Reply to this comment 猪小猪 Says:

    mark~

  32. Jul 17th, 2010 / 3:50 pm Reply to this comment booqoo Says:

    试试 http://wordpress.org/extend/plugins/regenerate-thumbnails/

  33. Jul 23rd, 2010 / 12:17 am Reply to this comment underone Says:

    @booqoo: 这就去!

  34. Jul 27th, 2010 / 11:35 pm Reply to this comment booqoo Says:

    如何?

  35. Jul 29th, 2010 / 12:37 pm Reply to this comment underone Says:

    @booqoo: 遇到了神奇无解的bug,运行完,有一部分缩略图变成中等图的格式了...

Leave a Reply