我们在制作(或DIY)WordPress主题的时候,特别是首页和分类目录页,我们就有必要通过控制文章摘要内容和标题的字数来让整体布局更加合理和美观,今天我们就跟大家分享几种方法完美截取WordPress文章摘要内容和标题字数。
方法一:使用WP内置函数wp_trim_words()截取
WordPress内置的wp_trim_words()函数,专门用来截取限定字数的内容,比如文章摘要、内容、标题等。
代码 | <?php echo wp_trim_words( get_the_content(), 66 ); // 文章内容 echo wp_trim_words( get_the_excerpt(), 66 ); // 文章摘要 echo wp_trim_words( get_the_title(), 30 ); // 文章标题 ?> |
wp_trim_words()函数默认用法:
代码 | <?php $trimmed = wp_trim_words( $text , $num_words = 55, $more = null ); ?> |
参数说明:
$text(字符串) (必需) 要截取的内容,默认: 无;
$num_words(整数) (可选) 限定的字数,默认: 55;
$more(字符串) (可选) 截取后加在尾部的字符,默认: ‘…’
示例说明:
代码 | <?php $content = get_the_content(); $trimmed_content = wp_trim_words( $content , 30, '<a href="' . get_permalink() . '"> ...阅读更多</a>' ); echo $trimmed_content ; ?> |
注:可以修改上面的数字30来设定长度。
方法二:使用php函数mb_strimwidth()截取
mb_strimwidth是超轻量级的php函数,用来获取指定的宽度截断字符串。
mb_strimwidth()函数默认用法:
代码 | mb_strimwidth ( string $str , int $start , int $width [, string $trimmarker [, string $encoding ]] ) |
参数说明:
$str //指定字符串
$start //指定从何处开始截取
$width //截取文字的宽度
$trimmarker //超过
$width数字后显示的字符串
示例说明:
平时我们调用文章标题都是这样:
代码 | <?php the_title(); ?> |
现在我想控制标题的输出字数,只需要使用mb_strimwidth函数后变成这样:
代码 | <?php echo mb_strimwidth(get_the_title(), 0, 30, "..." ); ?> |
注:可以修改上面的数字30来设定长度。
方法三:使用原生函数customTitle ()截取
将下面的代码添加到主题的functions.php文件最后一个 ?> 的前面:
代码 | function customTitle( $limit ) { $title = get_the_title( $post ->ID); if ( strlen ( $title ) > $limit ) { $title = substr ( $title , 0, $limit ) . '...' ; } echo $title ; } |
然后在需要调用的地方添加下面的代码即可:
代码 | <?php customTitle(30); ?> |
注:可以修改上面的数字30来设定长度。
方法四:使用自定义函数cut_str ()截取
将下面的代码添加到主题的functions.php文件最后一个 ?> 的前面:
代码 | //标题截断 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 ;}; |
然后在需要调用的地方添加下面的代码即可:
代码 | <?php echo cut_str( $post ->post_title,30); ?> |
注:可以修改上面的数字30来设定长度。
方法五:使用CSS代码来“截取”
其实这不是截取,而是隐藏了溢出的字符。我们可以在主题CSS文件style.css中对标题所在的选择器 id 或 class 添加下面的样式:
代码 | .post-title{ width:250px; /* 限制宽度(可选) */ whitewhite-space:nowrap; /* 禁止自动换行 */ word- break :keep-all; /* 这个也是禁止自动,二选一即可 */ overflow:hidden; /* 隐藏溢出的内容 */ text-overflow:ellipsis; /* 溢出文本使用...代替 */ } |
总结
文中分享的五种方法都可以实现自动截取WordPress文章的内容、摘要和标题字数,至于那个方法比较好用就是仁者见仁智者见智了,boke112在使用的过程中都是结合几种方法来使用的,比如同时使用方法二、四、五,有些地方可能是用方法二,有些地方可能是用方法五,关键还是看自己喜欢吧。
Knowledge知识 Record记录 WordPress文章摘要内容和标题字数截取方法
历史上的今天 ( 13 ):
- 2024年-05月-09日:News新闻:2024年5月9日新闻简报
- 2022年-05月-09日:Global国外名家:达芬奇作品全集赏析 (202P)
- 2022年-05月-09日:益智游戏:一笔连成游戏
- 2022年-05月-09日:益智游戏:手机连线游戏
- 2022年-05月-09日:冒险闯关:方块英雄游戏
- 2022年-05月-09日:冒险闯关:圣诞老人过桥游戏
- 2022年-05月-09日:运动休闲:切水果游戏
- 2022年-05月-09日:Global国外合集:19世纪后印象派名画赏析 (15P)
- 2022年-05月-09日:运动休闲:萌萌水贼游戏
- 2022年-05月-09日:运动休闲:两只动物赛跑游戏
- 2022年-05月-09日:Region地理:河南省人文地图(旅游必备) (18P)
- 2022年-05月-09日:Gods神明:史上最全佛图 (45P)
- 2022年-05月-09日:Region地理:全国省会城市标志性建筑 (34P)
可点 ➠ 2023年-05月-09日 ➠ 67 s ➠ ♥ 0