怎样在wordpress中显示摘要和缩略图

Posted on 十一月 21st, 2009 by CT in 前端 | 0 comments
Tags: , , ,

这篇文章不算真正的原创,我是根据国外一篇文 章与国内一篇文章写 的,因为用中文搜索了好几种方法与插件,效果都不如意(同宿舍的说:RP问题),根据coder+的文章实现摘要与缩略图的效果,根据javaws专栏的 文章解决首页博客摘要中文字数显示问题。还有我用的是wordpress 2.8.6,别的版本的wp没试过。

下面是操作方法:

1.显示摘要

找开你在用的主题的文件夹,找到index.php这个文件,找开<?php the_content(‘Continue reading…’); ?>也有可能是<?php the_content();  ?>,然后把其修改为<?php the_excerpt(); ?>

2.Timthumb:

这 里

点View Source Code,然后在找开的网页中右键timthumb.php

3.timthumb和cache:

在www文件中的wp-content文件夹中创建一个thumbnails文件,把刚才下载下来的timthumb.php文件上传进去。然后在 thumbnails文件夹中创建一个文件夹cache,并把cache的文件属性改为777。

4.thumbnail generation+display:

把下面的thumbnail generation+display代码加到第一步中的<?php the_excerpt(); ?>上面,也就是你修改的index.php文件中的<?php the_excerpt(); ?>上面,代码如下:

<?php  $custom_values = get_post_custom_values("thumb_source");
   if (isset($custom_values[0]))  $image_source= $custom_values[0];
  else
  {$id =$post->ID;
  $the_content =$wpdb->get_var("SELECT post_content FROM   $wpdb->posts WHERE ID = $id");
  $pattern = '!<img.*?src="(.*?)"!';
  preg_match_all($pattern, $the_content, $matches);
  $image_source = $matches['1'][0];
  }if (isset($image_source))
  {?>
  <a href="<?php the_permalink();?>"><img src="http://www.yourblog.com/wp-content/thumbnails/timthumb.php?src=<?php echo $image_source;?>&w=180&h=180&zc=1&q=85"  alt="<?php the_title();?>"></a>
  <?php }?>

图片如下:

5.修改:

把上面加上去的代码中的http://www.yourblog.com改为你的博客地址,上面的图片中我已经把它 改为这个博客的网址http://focusec.cn了,里面还有一行是&w=180&h=180&zc=1&q=85, 其中w=180是把缩略图的宽度是180,h=180指高度是180,sz=1是指当图片太大时将其修剪后再显示,q=85是指缩略图显示质量。你可以改 w,h这两个参数,尽量不要改sz和q.

6.style.css:

把缩略图的样式加到style.css中,找开博客的控制板,在外观中编辑style.css,在最后加上去下面的样式代码

.cute-post-thumbnail{float:left;width:190px;height:190px;padding:10px;margin-right:10px;margin-bottom:10px;background:#d5d6d7;border: 3px solid #ccc;}

7.中文字数显示:

下载中文 WordPress 工具箱(地址:http://yan.me/dia/wordpress/kit?q=blog/wordpress/kit),下载后解压缩,找开找到下面的代码

function mul_excerpt ($excerpt) {
$myexcerpt = substr($excerpt,0,255);
return utf8_trim($myexcerpt) . ‘… ‘;
}

add_filter(‘the_excerpt’, ‘mul_excerpt’);
add_filter(‘the_excerpt_rss’, ‘mul_excerpt’);

默认为255个字符,你可以修改为你需要的文字,我的代码如下:

然后把它上传到你的wordpress安装目录下的wp-content/plugins目录下(这 样就安装了中文 WordPress 工具箱),然后把这个插件激活就可以了。

完成的效果可以查看我们博客的首页:http://www.focusec.cn
也可以看一下默认主题下的显示效果

这个方法只能显示本站的图片为缩略图,另外还有进一下的设置,可以访问coder+文 章

Leave a Reply