|
在一Blog发现的插件,作者把源码给出来了,
原文:
Truncate Titles
Let’s say you’ve written lots of posts with lengthy titles. But now you’re switching to a theme that doesn’t handle long titles well. Copy this into a file, name it truncateTitle.php (or whatever.php), upload to your plugins directory, and activate it in the usual way. Don’t leave any blank lines or spaces before or after this code (or you’ll get an error).- <?php
- /*
- Plugin Name: KB Truncate Title
- Plugin URI: http://adambrown.info/b/widgets/category/custom/
- Description: Truncates titles so they don't break your theme
- Version: 1.0
- Author: Adam R. Brown
- Author URI: http://adambrown.info/
- */
- define('KB_TRUNCATE_TITLE', 50); // change 50 to max number of characters to allow
- function kb_truncateTitle($title){
- if (!is_int( KB_TRUNCATE_TITLE ) || 1>KB_TRUNCATE_TITLE )
- return $title;
- // rather than just use substr, let's make sure we cut it at the end of a word
- $arr = explode(' ', $title);
- $out = '';
- $count = 0;
- foreach( $arr as $str ){
- $count += ( strlen($str) + 1); // +1 is for the space we removed
- if ($count > KB_TRUNCATE_TITLE)
- break;
- $out .= $str . ' ';
- }
- // make sure we got SOMEthing
- if (!$out)
- $out = $arr[0];
- return $out;
- }
复制代码 This entry was written by Adam on January 7, 2008. This entry was posted in Custom. Bookmark the permalink. Both comments and trackbacks are currently closed.
这个插件的好处是不会把单词从中间间断,它会在接近限定的单词数范围内把最后一个单词截断。
用Google翻译也都能知道这大概说什么,不过我顺便说下吧,新建一个文件truncateTitle.php ,把代码复制进去,注意不要有空格,要不会出错,压缩成zip文件上传激活就可以了,不过我在后台看不见插件,卸载麻烦,考虑清楚再安装。 小弟不才,不正之处,还望指点一二。 |
|