字符摘录
4 个回答
- 投票数
-
- 2012-10-16
我在我的最后一个项目中使用了此代码:
function ng_get_excerpt( $count ){ $permalink = get_permalink( $post->ID ); $excerpt = get_the_content(); $excerpt = strip_tags( $excerpt ); $excerpt = mb_substr( $excerpt, 0, $count ); $excerpt = mb_substr( $excerpt, 0, strripos( $excerpt, " " ) ); $excerpt = rtrim( $excerpt, ",.;:- _!$&#" ); $excerpt = $excerpt . '<a href="'.$permalink.'" style="text-decoration: none;"> (...)</a>'; return $excerpt; }
我从这里得到的:
http://wordpress.org/support/topic/limit -excerpt-length-characters
它的优点是不允许在末尾使用标点符号,并以最后一个完整的单词结尾
使用 @medhamza7 或 @bainternet 或 @fuxia .>
I used this code in one of my last projects:
function ng_get_excerpt( $count ){ $permalink = get_permalink( $post->ID ); $excerpt = get_the_content(); $excerpt = strip_tags( $excerpt ); $excerpt = mb_substr( $excerpt, 0, $count ); $excerpt = mb_substr( $excerpt, 0, strripos( $excerpt, " " ) ); $excerpt = rtrim( $excerpt, ",.;:- _!$&#" ); $excerpt = $excerpt . '<a href="'.$permalink.'" style="text-decoration: none;"> (...)</a>'; return $excerpt; }
I got it from here:
http://wordpress.org/support/topic/limit-excerpt-length-by-characters
It has the advantage of not allowing punctuation on the end and ending with the last complete word
Using the filters as suggested by @medhamza7 or @bainternet or @fuxia is preferable.
-
非常感谢您的建议,对我来说高效的建议很简单:"echo substr(get_the_excerpt(),0,30);"摘自您的链接.Very thanks for this advice, efficient one for me is just simple: "echo substr(get_the_excerpt(), 0,30);" taken from Your links.
- 0
- 2012-10-16
- Marcin
-
@Marcin [`substr()`会中断](http://wpengineer.com/2410/dont-use-strlen/).切勿在UTF-8编码的数据上使用它.@Marcin [`substr()` will break](http://wpengineer.com/2410/dont-use-strlen/). Never use it on UTF-8 encoded data.
- 0
- 2012-10-16
- fuxia
-
- 2012-10-16
使用此答案中的函数
utf8_truncate()
并逐步解决问题wp_trim_excerpt()
.示例代码,未经测试:
add_filter( 'excerpt_more', 'wpse_69436_excerpt_more' ); function wpse_69436_excerpt_more( $more ) { add_filter( 'wp_trim_excerpt', 'wpse_69436_trim_excerpt' ); // we remove the more text here return ''; } function wpse_69436_trim_excerpt( $excerpt ) { return utf8_truncate( $excerpt, 300 ); }
Use the function
utf8_truncate()
from this answer and fight your way throughwp_trim_excerpt()
.Sample code, not tested:
add_filter( 'excerpt_more', 'wpse_69436_excerpt_more' ); function wpse_69436_excerpt_more( $more ) { add_filter( 'wp_trim_excerpt', 'wpse_69436_trim_excerpt' ); // we remove the more text here return ''; } function wpse_69436_trim_excerpt( $excerpt ) { return utf8_truncate( $excerpt, 300 ); }
-
- 2012-10-16
WordPress有一个针对该过滤器的过滤器,方便地命名为excerpt_length,它接受许多字符,因此:
function custom_excerpt_length( $length ) { return 50; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
将50更改为所需的上限.
每个@toscho评论的更新:
上面的解决方案也适用于单词,而不适用于字符,因此这里是一种快速解决方案:
add_filter('the_excerpt','excerpt_char_limit'); function excerpt_char_limit($e){ return substr($e,0,50); }
WordPress has a filter for that which is conveniently named excerpt_length and it accepts a number of chars so:
function custom_excerpt_length( $length ) { return 50; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
change 50 to whatever limit you want.
Update per @toscho comment:
that is the solution above is for words as well and not for chars so here is a quick one:
add_filter('the_excerpt','excerpt_char_limit'); function excerpt_char_limit($e){ return substr($e,0,50); }
-
那不是字符,而是词.That’s not for characters, it is for words.
- 1
- 2012-10-16
- fuxia
-
@toscho我不好!用一个简单的解决方案更新@toscho My bad! updated with a simple solution
- 0
- 2012-10-16
- Bainternet
-
如何运作?我把它作为"echo custom_excerpt_length();",但是它不起作用.How does it works? i put it as "echo custom_excerpt_length(); " but it doesn't work.
- 0
- 2012-10-16
- Marcin
-
谢谢,现在可以使用了,但是如何为首页提供一些摘录长度呢?Thanks, now it works, but how to give some excerpts lengths for front page?
- 0
- 2012-10-16
- Marcin
-
@Bainternet-我给了:" add_filter('the_excerpt2','excerpt_char_limit2'); 函数excerpt_char_limit2($e){ 返回substr($e,0,10); }",但不起作用.@Bainternet - I gave: "add_filter('the_excerpt2','excerpt_char_limit2'); function excerpt_char_limit2($e){ return substr($e,0,10); }" but it doesn't work.
- 0
- 2012-10-16
- Marcin
-
- 2017-11-06
为更好的方法,可以使用
get_the_excerpt
过滤器:function get_excerpt($excerpt="",$limit=140){ $excerpt = preg_replace(" (\[.*?\])",'',$excerpt); $excerpt = strip_shortcodes($excerpt); $excerpt = strip_tags($excerpt); $excerpt = mb_substr($excerpt, 0, $limit); $excerpt = mb_substr($excerpt, 0, strripos($excerpt, " ")); $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt)); $excerpt = $excerpt.'...'; return $excerpt; } add_filter('get_the_excerpt',"get_excerpt");
将
$limit=140
更改为所需的字符数.另外,如果您想以其他方式:add_filter('get_the_excerpt',function ($excerpt="",$limit=140){ $excerpt = preg_replace(" (\[.*?\])",'',$excerpt); $excerpt = strip_shortcodes($excerpt); $excerpt = strip_tags($excerpt); $excerpt = mb_substr($excerpt, 0, $limit); $excerpt = mb_substr($excerpt, 0, strripos($excerpt, " ")); $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt)); $excerpt = $excerpt.'...'; return $excerpt; });
这样可以避免发生任何冲突,例如函数
get_excerpt
的现有名称.For a better way, you can use the
get_the_excerpt
filter:function get_excerpt($excerpt="",$limit=140){ $excerpt = preg_replace(" (\[.*?\])",'',$excerpt); $excerpt = strip_shortcodes($excerpt); $excerpt = strip_tags($excerpt); $excerpt = mb_substr($excerpt, 0, $limit); $excerpt = mb_substr($excerpt, 0, strripos($excerpt, " ")); $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt)); $excerpt = $excerpt.'...'; return $excerpt; } add_filter('get_the_excerpt',"get_excerpt");
Change the
$limit=140
to the number of characters you want. Also if you want in different way:add_filter('get_the_excerpt',function ($excerpt="",$limit=140){ $excerpt = preg_replace(" (\[.*?\])",'',$excerpt); $excerpt = strip_shortcodes($excerpt); $excerpt = strip_tags($excerpt); $excerpt = mb_substr($excerpt, 0, $limit); $excerpt = mb_substr($excerpt, 0, strripos($excerpt, " ")); $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt)); $excerpt = $excerpt.'...'; return $excerpt; });
That will make avoid any conflict like existing name of function
get_excerpt
.-
@Nicolai我认为我们应该避免使用mb_substr,因为默认情况下某些php配置不会激活mb功能:/@Nicolai i think we should avoid using `mb_substr` cause some php config by default doesn't activated mb functions :/
- 0
- 2018-02-26
- med amine hamza
-
如[here](https://wordpress.stackexchange.com/a/11089/22534)所述,WP具有后备功能.As read [here](https://wordpress.stackexchange.com/a/11089/22534), WP has a fallback for it.
- 0
- 2018-02-26
- Nicolai
我在functions.php中有代码:
但是我需要限制摘录的字符数, 你能帮我吗?