原本博客右边的最新文章和最新评论不限字数,所以带来一个问题:当标题过长或者评论过长,就会换行显示,如此
一个标题或者评论占了两行,不太美观。于是搜索限制字数显示的文章。在 wordpress首页显示摘要的几种方法 页
找到的方法,同样适用于我现在的情况,把以下代码
- function dm_strimwidth($str ,$start , $width ,$trimmarker ){
- $output = preg_replace('/^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start.'}((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$width.'}).*/s','\1',$str);
- return $output.$trimmarker;
- }
复制到functions.php中,然后在用到标题及评论的页面内修改,由于主题不用,使用方法也不用。拿我的举例吧。
代码在plugins.php中
- foreach ($comments as $comment) {
- $output .= "\n\t
- <li><span>" . $comment->comment_author . ':</span> <a title="on ' . $comment->post_title . '" href="' . get_permalink($comment->ID) . '#comment-' . $comment->comment_ID . '">' . strip_tags($comment->com_excerpt) . '...</a></li>
- ';
- }
在右边输出评论内容然后我修改为
- foreach ($comments as $comment) {
- $output .= "\n\t
- <li><span>" . $comment->comment_author . ':</span> <a title="on ' . $comment->post_title . '" href="' . get_permalink($comment->ID) . '#comment-' . $comment->comment_ID . '">' . mb_strimwidth(strip_tags($comment->com_excerpt),0,36) . '...</a></li>
- ';
- }
也就是添加了mb_strimwidth( ,0,36),36可以自行调整。
经验之谈,不错。
[回复]
没用过!必要性也不太大啊!
[回复]
Danfi 回复:
2010-08-02 23:08
有些主题是不必要,我的那个,不用会使文章下沉
[回复]
正在使用中
[回复]