在主题的制作过程中,需要用到自动截取限定字数的文章摘要,虽然,有些人使用 more 标签来截取,也有些人会自己给每篇文章填写摘要,然后使用 the_excerpt() 函数输出, 但并不是所有的朋友都有这些习惯。而且,他们自己截取的摘要,可能超过了主题的样式限制,比如下图,摘要的字数太多,超出了主题样式的设置,看上去很不爽。

所以,做大众主题的我们,需要自动截取限定字数的摘要,满足自己设置样式的需要。
WordPress 3.3 新增了一个 wp_trim_words() 函数,专门用来截取限定字数的内容,比如文章、摘要、标题等,使用方法请看:
https://www.wpdaxue.com/wp_trim_words.html
===============不再推荐下面的方法========================
除了插件以外,用得比较多的是下面的代码,是使用php本身的 mb_strimwidth 函数实现的,在需要显示摘要的地方使用下面的代码:
|
|
<span style="color: #000000; font-weight: bold;"><?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">mb_strimwidth</span><span style="color: #009900;">(</span><span style="color: #990000;">strip_tags</span><span style="color: #009900;">(</span>apply_filters<span style="color: #009900;">(</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-></span><span style="color: #004000;">post_content</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span><span style="color: #0000ff;">"..."</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?></span> |
|
<?php echo mb_strimwidth(strip_tags(apply_filters(‘the_content’, $post->post_content)), 0, 200,"…"); ?>
上面的数字 200,可以根据自己的需要来修改。
但是,如果有些主机空间不支持 mb_strimwidth 函数怎么办?那可以使用下面的方法实现效果。
在主题的 functions.php 文件最后一个 ?> 前面添加下面的函数
|
|
<span style="color: #666666; font-style: italic;">//摘要截断</span> <span style="color: #000000; font-weight: bold;">function</span> dm_strimwidth<span style="color: #009900;">(</span><span style="color: #000088;">$str</span> <span style="color: #339933;">,</span><span style="color: #000088;">$start</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">,</span><span style="color: #000088;">$trimmarker</span> <span style="color: #009900;">)</span><span style="color: #009900;">{</span> <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'/^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'</span><span style="color: #339933;">.</span><span style="color: #000088;">$start</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'}((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'</span><span style="color: #339933;">.</span><span style="color: #000088;">$width</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'}).*/s'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'1'</span><span style="color: #339933;">,</span><span style="color: #000088;">$str</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">.</span><span style="color: #000088;">$trimmarker</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> |
|
//摘要截断
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;
}
然后在需要显示摘要的地方使用下面的代码调用即可:
|
|
<span style="color: #000000; font-weight: bold;"><?php</span> <span style="color: #b1b100;">echo</span> dm_strimwidth<span style="color: #009900;">(</span><span style="color: #990000;">strip_tags</span><span style="color: #009900;">(</span><span style="color: #000088;">$post</span><span style="color: #339933;">-></span><span style="color: #004000;">post_content</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span><span style="color: #0000ff;">"..."</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?></span> |
|
<?php echo dm_strimwidth(strip_tags($post->post_content),0,200,"…"); ?>
同样,上面的 200 ,也可以根据自己的需要修改。
如果你有更加完美的实现代码,欢迎留言与我们交流。
赏 如果本文对你有帮助,请打赏作者,鼓励我们继续写作!
请先
!