WordPress中自动截取限定字数的文章摘要

前端之家收集整理的这篇文章主要介绍了WordPress中自动截取限定字数的文章摘要前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

制作的过程当中,我们经常需要截取限定字数的文章摘要,有些朋友喜欢使用 more 标签来截取,还有一些朋友喜欢手动给每篇文章添加摘要,然后使用 the_excerpt() 函数输出,但是这样耗时耗力,而且效果也不是特别好。91wordpress的站长结合多年wordpress制作经验,向大家介绍一个简单的方法:

1. 切换到主题所在目录,打开functions.php,加入如下代码:

',']]>',$text)); $text = trim($text); if (strlen($text) > $max_char) { $text = substr($text,$max_char+1); $text = utf8_conver($text); $text = str_replace(array("\r","\n"),' ',$text); $text .= $more_text; if ($limit_type == 'content'){ $text = "

".$text."

"; $text .= "
"; } echo $text; } else { if ($limit_type == 'content'){$text = "

".$text."

";} echo $text; } } function utf8_conver($str) { $len = strlen($str); for ($i=strlen($str)-1; $i>=0; $i-=1){ $hex .= ' '.ord($str[$i]); $ch = ord($str[$i]); if (($ch & 128)==0) return(substr($str,$i)); if (($ch & 192)==192) return(substr($str,$i)); } return($str.$hex); } ?>
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891d5588e31753058549-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-2">2
Crayon-num" data-line="Crayon-5c891d5588e31753058549-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-4">4
Crayon-num" data-line="Crayon-5c891d5588e31753058549-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-6">6
Crayon-num" data-line="Crayon-5c891d5588e31753058549-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-8">8
Crayon-num" data-line="Crayon-5c891d5588e31753058549-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-10">10
Crayon-num" data-line="Crayon-5c891d5588e31753058549-11">11
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-12">12
Crayon-num" data-line="Crayon-5c891d5588e31753058549-13">13
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-14">14
Crayon-num" data-line="Crayon-5c891d5588e31753058549-15">15
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-16">16
Crayon-num" data-line="Crayon-5c891d5588e31753058549-17">17
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-18">18
Crayon-num" data-line="Crayon-5c891d5588e31753058549-19">19
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-20">20
Crayon-num" data-line="Crayon-5c891d5588e31753058549-21">21
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-22">22
Crayon-num" data-line="Crayon-5c891d5588e31753058549-23">23
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-24">24
Crayon-num" data-line="Crayon-5c891d5588e31753058549-25">25
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-26">26
Crayon-num" data-line="Crayon-5c891d5588e31753058549-27">27
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-28">28
Crayon-num" data-line="Crayon-5c891d5588e31753058549-29">29
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-30">30
Crayon-num" data-line="Crayon-5c891d5588e31753058549-31">31
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-32">32
Crayon-num" data-line="Crayon-5c891d5588e31753058549-33">33
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-34">34
Crayon-num" data-line="Crayon-5c891d5588e31753058549-35">35
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d5588e31753058549-36">36
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891d5588e31753058549-1">Crayon-ta">PHPCrayon-h">
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-2">Crayon-c">/*added by jacky*/
Crayon-line" id="Crayon-5c891d5588e31753058549-3">Crayon-t">functionCrayon-h"> Crayon-e">jackyexcerptCrayon-sy">(Crayon-v">$max_charCrayon-h"> Crayon-o">=Crayon-h"> Crayon-cn">200Crayon-sy">,Crayon-h"> Crayon-v">$more_textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">'...'Crayon-sy">,Crayon-h"> Crayon-v">$limit_typeCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">'content'Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-4">
Crayon-line" id="Crayon-5c891d5588e31753058549-5">Crayon-h">Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-v">$limit_typeCrayon-h"> Crayon-o">==Crayon-h"> Crayon-s">'title'Crayon-sy">)Crayon-h"> Crayon-sy">{Crayon-h"> Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">get_the_titleCrayon-sy">(Crayon-sy">)Crayon-sy">;Crayon-h"> Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-6">Crayon-h">Crayon-st">elseCrayon-h"> Crayon-sy">{Crayon-h"> Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">get_the_contentCrayon-sy">(Crayon-sy">)Crayon-sy">;Crayon-h"> Crayon-sy">}
Crayon-line" id="Crayon-5c891d5588e31753058549-7">Crayon-h">Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">apply_filtersCrayon-sy">(Crayon-s">'the_content'Crayon-sy">,Crayon-h"> Crayon-v">$textCrayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-8">Crayon-h">Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">strip_tagsCrayon-sy">(Crayon-e">str_replaceCrayon-sy">('Crayon-sy">,Crayon-h"> 'Crayon-sy">,Crayon-h"> Crayon-v">$textCrayon-sy">)Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d5588e31753058549-9">Crayon-h"> Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">trimCrayon-sy">(Crayon-v">$textCrayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-10">Crayon-h">Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-e">strlenCrayon-sy">(Crayon-v">$textCrayon-sy">)Crayon-h"> Crayon-h"> Crayon-v">$max_charCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891d5588e31753058549-11">Crayon-h"> Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">substrCrayon-sy">(Crayon-v">$textCrayon-sy">,Crayon-h"> Crayon-cn">0Crayon-sy">,Crayon-h"> Crayon-v">$max_charCrayon-o">+Crayon-cn">1Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-12">Crayon-h">Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">utf8_converCrayon-sy">(Crayon-v">$textCrayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d5588e31753058549-13">Crayon-h"> Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">str_replaceCrayon-sy">(Crayon-t">arrayCrayon-sy">(Crayon-s">"\r"Crayon-sy">,Crayon-h"> Crayon-s">"\n"Crayon-sy">)Crayon-sy">,Crayon-h"> Crayon-s">' 'Crayon-sy">,Crayon-h"> Crayon-v">$textCrayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-14">Crayon-h"> Crayon-v">$textCrayon-h"> Crayon-sy">.Crayon-o">=Crayon-h"> Crayon-v">$more_textCrayon-sy">;
Crayon-line" id="Crayon-5c891d5588e31753058549-15">Crayon-h"> Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-v">$limit_typeCrayon-h"> Crayon-o">==Crayon-h"> Crayon-s">'content'Crayon-sy">)Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-16">Crayon-h"> Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">"

"Crayon-sy">.Crayon-v">$textCrayon-sy">.Crayon-s">"

"
Crayon-sy">;
Crayon-line" id="Crayon-5c891d5588e31753058549-17">Crayon-h">Crayon-v">$textCrayon-h"> Crayon-sy">.Crayon-o">=Crayon-h"> Crayon-s">""Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-18">Crayon-h"> Crayon-sy">}
Crayon-line" id="Crayon-5c891d5588e31753058549-19">Crayon-h">Crayon-k ">echoCrayon-h"> Crayon-v">$textCrayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-20">Crayon-h">Crayon-sy">}Crayon-h"> Crayon-st">elseCrayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891d5588e31753058549-21">Crayon-h"> Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-v">$limit_typeCrayon-h"> Crayon-o">==Crayon-h"> Crayon-s">'content'Crayon-sy">)Crayon-sy">{Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">"

"Crayon-sy">.Crayon-v">$textCrayon-sy">.Crayon-s">"

"
Crayon-sy">;Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-22">Crayon-h">Crayon-k ">echoCrayon-h"> Crayon-v">$textCrayon-sy">;
Crayon-line" id="Crayon-5c891d5588e31753058549-23">Crayon-h">Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-24">Crayon-sy">}
Crayon-line" id="Crayon-5c891d5588e31753058549-25">
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-26">Crayon-t">functionCrayon-h"> Crayon-e">utf8_converCrayon-sy">(Crayon-v">$strCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891d5588e31753058549-27">Crayon-h"> Crayon-v">$lenCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">strlenCrayon-sy">(Crayon-v">$strCrayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-28">Crayon-h"> Crayon-st">forCrayon-h"> Crayon-sy">(Crayon-v">$iCrayon-o">=Crayon-e">strlenCrayon-sy">(Crayon-v">$strCrayon-sy">)Crayon-o">-Crayon-cn">1Crayon-sy">;Crayon-h"> Crayon-v">$i=Crayon-cn">0Crayon-sy">;Crayon-h"> Crayon-v">$iCrayon-o">-=Crayon-cn">1Crayon-sy">)Crayon-sy">{
Crayon-line" id="Crayon-5c891d5588e31753058549-29">Crayon-h"> Crayon-v">$hexCrayon-h"> Crayon-sy">.Crayon-o">=Crayon-h"> Crayon-s">' 'Crayon-sy">.Crayon-e">ordCrayon-sy">(Crayon-v">$strCrayon-sy">[Crayon-v">$iCrayon-sy">]Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-30">Crayon-h"> Crayon-v">$chCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">ordCrayon-sy">(Crayon-v">$strCrayon-sy">[Crayon-v">$iCrayon-sy">]Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d5588e31753058549-31">Crayon-h"> Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-sy">(Crayon-v">$chCrayon-h"> Crayon-o">&Crayon-h"> Crayon-cn">128Crayon-sy">)Crayon-o">==Crayon-cn">0Crayon-sy">)Crayon-h"> Crayon-k ">returnCrayon-sy">(Crayon-e">substrCrayon-sy">(Crayon-v">$strCrayon-sy">,Crayon-cn">0Crayon-sy">,Crayon-v">$iCrayon-sy">)Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-32">Crayon-h"> Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-sy">(Crayon-v">$chCrayon-h"> Crayon-o">&Crayon-h"> Crayon-cn">192Crayon-sy">)Crayon-o">==Crayon-cn">192Crayon-sy">)Crayon-h"> Crayon-k ">returnCrayon-sy">(Crayon-e">substrCrayon-sy">(Crayon-v">$strCrayon-sy">,Crayon-v">$iCrayon-sy">)Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d5588e31753058549-33">Crayon-h"> Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-34">Crayon-h"> Crayon-k ">returnCrayon-sy">(Crayon-v">$strCrayon-sy">.Crayon-v">$hexCrayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d5588e31753058549-35">Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d5588e31753058549-36">

2. 在需要显示摘要的地方使用下面的代码调用即可:

Crayon-5c891d5588e43943610260" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-mixed-highlight" title="Contains Mixed Languages">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891d5588e43943610260-1">1
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891d5588e43943610260-1">Crayon-ta">PHPCrayon-h"> Crayon-e">jackyexcerptCrayon-sy">(Crayon-cn">200Crayon-sy">)Crayon-sy">;

注意:上面的 200 ,可以根据自己的需要修改

原文链接:https://www.f2er.com/wordpress/238521.html

猜你在找的wordpress相关文章