终于告一段落,wordpress的缩略图小研究

放弃使用timthumb这个史上最强的缩略图插件之后,哥陷在wordpress缩略图的小坑里着实是挣扎了一大番啊,从这里(http://yjyj.net/learn/wordpress-learn/4835.html)开始,哥就在折腾,直到今天在@炖哥的提示下,才发现真正问题所在
呕漏,现在反过头回去看看,当初自己只要认真些看文档,不要总是自己想当然,也许一周就可以解决问题.
为了让其他像哥一样对php只通半窍,却又喜欢折腾的基友

0.首先在function.php加入:

//使用缩略图设置
if (function_exists('add_image_size') && function_exists('add_image_size')) 
{
    add_theme_support('post-thumbnails');
    set_post_thumbnail_size( 196, 9999 );
    add_image_size( 'pic-thumb', 196, 9999, false );
}

1.调用可以使用以下代码:

<a class="img a" href="<?php the_permalink(); ?>#hi" title="<?php the_title(); ?>" target="_blank">
				<?php if (has_post_thumbnail( $post->ID ) ) {
		        $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'pic-thumb');
				 ?>
				<img src="<?php echo $thumbnail[0]; ?>" alt="<?php echo trim(strip_tags( $post->post_title )) ?>" width="<?php echo $thumbnail[1]; ?>" height="<?php echo $thumbnail[2]; ?>"/>
				</a>
				<?php } else { ?>
				<a class="noimg a" href="<?php the_permalink(); ?>#hi" title="<?php the_title(); ?>" target="_blank"><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 300,"...");?></a>
				<?php } ?>

转一篇好文:在VPS的SSH下批量删除WordPress缩略图

此前所有的教程都讲了一个避免WordPress产生缩略图的方法:

进入WordPress 后台管理,设置 -> 媒体选项,将图像大小所有数值都改为 0,保存

最新版的WordPress这样做没有问题,不会产生缩略图,但是如果是一些老站点,以前的WordPress版本由于各种bug问题,不可避免地会产生N多缩略图,一个个文件夹去删显然是不理智的

现在我们拥有了vps,意味着我们拥有了ssh shell,做批量删除这种事情简直就是so easy

WordPress自动产生的缩略图大小基本上是 “2位数/3位数*2位数/3位数”,写个正则就可以了

不过为了安全,首先我们要让shell进入WordPress的图片上传文件夹下

cd ./wp-content/uploads

然后使用find命令配合正则表达式查找一下是否有缩略图

find . -name '*-[0-9][0-9]x[0-9][0-9]*'

不出意外应该会出现一大堆….现在删除他们

find . -name '*-[0-9][0-9]x[0-9][0-9]*' -exec rm -f {} \;

这样就可以一次性把所有的缩略图全部干掉了

来自:http://www.slyar.com/blog/ssh-clear-wordpress-thumbnail.html

小 虾

哦也,我是小虾

You may also like...