给你的wordpress文章添加缩略图

2010年1月27日 | 分类: 代码如诗 | 标签: , ,
 
 

创建日志缩略图我想很多wper都有用过这个方法,很多主题都自带这个功能,由于我的主题没有自带,但是我又想用这个功能那么该怎么办呢?用插件?这个倒是可以实现,网络上有很多插件可以实现这个功能大家搜索一下就可以了。如果想用代码实现这个功能的继续往下看吧。

一、准备工作

1.在你的主题目录下建立一个文件夹scripts,在该目录下建立一个子目录cache用于缩略图缓存,把这两个目录设为777。

如:yourdomain.com/wp-content/themes/your-theme/scripts/

yourdomain.com/wp-content/themes/your-theme/scripts/cache/

2.下载TimThumb.php并放入scripts文件夹

yourdomain.com/wp-content/themes/your-theme/scripts/timthumb.php

记得要设文件夹的属性为777。

二、插入调用代码

在你的主题目录下找到index.php或者home.php进行编辑,找到定义标题附近的地方插件代码。

这里提供3中不同的代码调用

1.只显示图片

<?php // This will show only the image. Alter the width and height (in both places) to your needs. ?>
 
<?php if ( get_post_meta($post->ID, 'thumb', true) ) { ?>
	<div class="postthumb">
		<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=150&w=150&zc=1" alt="<?php the_title(); ?>" width="100" height="57" />
	</div>
<?php } ?>

2.显示图片,图片链接到文章。

        <?php // This will show the image and link the image to the post. Alter the width and height (in both places) to your needs. ?>
 
        <?php if ( get_post_meta($post->ID, 'thumb', true) ) { ?>
	<div class="postthumb">
		<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=150&w=150&zc=1" alt="<?php the_title(); ?>" width="150" height="150" /></a>
	</div>
<?php } ?>

3.显示图片,图片链接地址自定

 

<?php 	// This will show the image and link it to anything you place into another custom field of "imglink". 
		// Alter the width and height (in both places) to your needs.  
  ?>
 
<?php if ( get_post_meta($post->ID, 'imglink', true) ) { ?>
	<div class="postthumb">
		<a href="<?php echo get_post_meta($post->ID, "imglink", $single = true); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php } ?><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=150&w=150&zc=1" alt="<?php the_title(); ?>"  /><?php if ( get_post_meta($post->ID, 'imglink', true) ) { ?></a><?php } ?>
	</div>
<?php } ?>

三、使用方法

1.在自定义字段值处填入你上传图片的地址

2.使用字段名称 thumb

3.使用第三种代码的朋友出了需要填 thumb 外还要填写一个 imglink 值。如图所示:

PS,不知道怎么回事我在本地测试用第二种方法可以实现的,在服务器上偏偏不行,图片显示不了…囧,难道服务器问题?现在只能不用TimThumb.php了,把它删除掉,也不用在主题目录里建文件夹了。这样唯一不好的就是不能自己截取缩略图,不过貌似wordpress自带有150*150的缩略图。囧~

QQREADERA743E3A4568AE6F2

QQREADER63B8199F35B961F3

  1. Firm CHINA CHINA Google Chrome Google Chrome 4.0.249.78 Windows Windows XP
    2010年2月11日23:26

    呵呵,俺是通过插件实现的

  2. Showfom UNITED STATES UNITED STATES Mozilla Firefox Mozilla Firefox 3.5.7 Windows Windows XP
    2010年2月11日00:56

    这样会很麻烦滴

  3. 真好网 CHINA CHINA Internet Explorer Internet Explorer 7.0 Windows Windows XP
    2010年2月10日22:27

    可以借鉴下这个方法,我没有用过缩略图,都是直接把图片裁剪小了。

  4. 小明 CHINA CHINA Google Chrome Google Chrome 5.0.317.2 Windows Windows 7
    2010年2月10日17:40

    不来个演示?

  5. 海天无影 CHINA CHINA Safari Safari 532.4 Windows Windows XP
    2010年2月10日10:51

    不错 蛮好的

  6. 济南租房 CHINA CHINA class="WB-OS">Internet Explorer Internet Explorer 6.0
    2010年2月10日10:14

    济南租房网(esf.jnol.cn)看过这篇文章了,真不错,顶一下。

  7. 浪漫小屋 CHINA CHINA Internet Explorer Internet Explorer 6.0 Windows Windows XP
    2010年2月9日18:18

    收藏了

  8. 水天一线 CHINA CHINA Internet Explorer Internet Explorer 6.0 Windows Windows XP
    2010年2月9日17:36

    :?: 我不懂

  9. 好名字要隐藏 CHINA CHINA Mozilla Firefox Mozilla Firefox 3.5.7 Windows Windows XP
    2010年2月7日12:37

    :razz: 订阅了你的博客。欢迎交流。

  10. 光卫 CHINA CHINA Internet Explorer Internet Explorer 6.0 Windows Windows XP
    2010年2月5日14:53

    看来我要试试了

评论分页