给你的wordpress文章添加缩略图
创建日志缩略图我想很多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


呵呵,俺是通过插件实现的
这样会很麻烦滴
可以借鉴下这个方法,我没有用过缩略图,都是直接把图片裁剪小了。
不来个演示?
我的首页都是啊~
不错 蛮好的
济南租房网(esf.jnol.cn)看过这篇文章了,真不错,顶一下。
收藏了
看来我要试试了