使用wp_trim_words()截取
function customTitle($limit) { $title = get_the_title($post->ID); if(strlen($title) > $limit) { $title = substr($title, 0, $limit) . '...'; } echo $title; }
可以使用下面的代码:
<?php customTitle(30); ?>
数字30代表要截取的字数
//标题截断 function cut_str($src_str,$cut_length){$return_str='';$i=0;$n=0;$str_length=strlen($src_str); while (($n<$cut_length) && ($i<=$str_length)) {$tmp_str=substr($src_str,$i,1);$ascnum=ord($tmp_str); if ($ascnum>=224){$return_str=$return_str.substr($src_str,$i,3); $i=$i+3; $n=$n+2;} elseif ($ascnum>=192){$return_str=$return_str.substr($src_str,$i,2);$i=$i+2;$n=$n+2;} elseif ($ascnum>=65 && $ascnum<=90){$return_str=$return_str.substr($src_str,$i,1);$i=$i+1;$n=$n+2;} else {$return_str=$return_str.substr($src_str,$i,1);$i=$i+1;$n=$n+1;} } if ($i<$str_length){$return_str = $return_str . '...';} if (get_post_status() == 'private'){ $return_str = $return_str . '(private)';} return $return_str;};
将上面的代码添加到主题的 functions.php 最后一个 ?> 的前面,然后在需要调用的地方添加下面的代码即可:
<?php echo cut_str($post->post_title,80); ?>
通过CSS来“截取”
.post-title{ width:250px; /* 限制宽度(可选) */ white-space:nowrap; /* 禁止自动换行 */ overflow:hidden; /* 隐藏溢出的内容 */ text-overflow:ellipsis; /* 溢出文本使用...代替 */ }
wordpress截取简介字数
<?PHP echo mb_strimwidth(strip_tags(apply_filters('the_excerpt', $post->post_content)), 0, 200,"···"); ?>
wordpress截取文章内容字数
<?PHP echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 200,"···"); ?>
通过修改functions.php文件
add_filter('the_content','substr_content'); function substr_content($content){ if(!is_singular()){ $content=mb_strimwidth(strip_tags($content),0,200); } return $content; }
其中”is_singular()”是条件判断函数,表示在除单页面外的首页或分类等列表页面应用该函数,”200″表示截取的字数,相当于200个英文字符或100个中文字符。
使用the_excerpt标签
使用方法,首先找到wp-content/themes下你使用的模板目录,查找目录中的文件,如果有home.php则修改home.php,没有的话就修改index.php,找到
<?php the_content(__(’(more…)’)); ?>
或
<?php the_content(); ?>
修改为:
<?php if(!is_single()) { the_excerpt(); } else { the_content(__(’(more…)’));//或者<?php the_content(); ?> } ?>
现在你的wordpress,除非打开单个post,其他情况下都是显示摘要。
这段代码可以在你的首页、存档页、目录页使用摘要输出,使用摘要输出后,整个WordPress的重复内容就少多了,很利于搜索引擎优化。
使用WP-utf8-excerpt插件
特点:
1、支持多字节语言(如中文),不会产生乱码;
2、首页每篇文章显示300字,存档页面每篇文章显示150字(字数可设置);
3、摘要可保留文章中的格式标签,如字体、颜色、链接、图片等(需保留的标签可在后台设置)。
博客后台搜索安装WP-utf8-excerpt插件,启用插件后在设置里点击 Excerpt 修改相应选项。
使用more标签
在撰写新文章时使用HTML编辑状态,在需要截断文章的地方插入more标签。More标签显示如下:
<!––more––>