Knowledge知识 Record记录 WordPress文章摘要内容和标题字数截取方法
►   站内排名:No.8520   ◄

我们在制作(或DIY)WordPress主题的时候,特别是首页和分类目录页,我们就有必要通过控制文章摘要内容和标题的字数来让整体布局更加合理和美观,今天我们就跟大家分享几种方法完美截取WordPress文章摘要内容和标题字数。

方法一:使用WP内置函数wp_trim_words()截取

WordPress内置的wp_trim_words()函数,专门用来截取限定字数的内容,比如文章摘要、内容、标题等。

代码<?phpechowp_trim_words( get_the_content(), 66 ); // 文章内容echowp_trim_words( get_the_excerpt(), 66 ); // 文章摘要echowp_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(字符串) (可选) 截取后加在尾部的字符,默认: ‘&hellip;’

示例说明

代码<?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 echomb_strimwidth(get_the_title(), 0, 30,"..."); ?>

注:可以修改上面的数字30来设定长度。

方法三:使用原生函数customTitle ()截取

将下面的代码添加到主题的functions.php文件最后一个 ?> 的前面:

代码functioncustomTitle($limit) {    $title= get_the_title($post->ID);    if(strlen($title) > $limit) {        $titlesubstr($title, 0, $limit) . '...';    }    echo$title;}

然后在需要调用的地方添加下面的代码即可:

代码<?php customTitle(30); ?>

注:可以修改上面的数字30来设定长度。

方法四:使用自定义函数cut_str ()截取

将下面的代码添加到主题的functions.php文件最后一个 ?> 的前面:

代码//标题截断functioncut_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 echocut_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 ):

可点 ➠ 2023年-05月-09日 67 s 0
 ♥   0
 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注