admin管理员组文章数量:1135139
I'm trying to build a theme, I want to control the length of the post excerpts by doing something like this in functions.php:
function theme_excerpt_length( $length ) {
return 45;
}
add_filter( 'excerpt_length', 'theme_excerpt_length', 999 );
But it doesn't seem to have the desired effect of reducing length of excerpt to 45 words. Even, without the function, some excerpts are longer than the default 55 words. What could be wrong?
I'm trying to build a theme, I want to control the length of the post excerpts by doing something like this in functions.php:
function theme_excerpt_length( $length ) {
return 45;
}
add_filter( 'excerpt_length', 'theme_excerpt_length', 999 );
But it doesn't seem to have the desired effect of reducing length of excerpt to 45 words. Even, without the function, some excerpts are longer than the default 55 words. What could be wrong?
Share Improve this question asked Apr 2, 2014 at 0:16 bodesambodesam 1631 gold badge2 silver badges10 bronze badges 2- 4 do those posts have a custom excerpt entered in the excerpt field? – Milo Commented Apr 2, 2014 at 0:38
- No. No custom excerpt. – bodesam Commented Apr 2, 2014 at 15:58
4 Answers
Reset to default 5The are two quick ways to display custom excerpt lengths in your theme using wp_trim_words
. Remember, if you use the_excerpt()
, your excerpt length will always be a maximum of 55, never more. If you use the_content()
on the other hand, you can specify an excerpt length of more than 55 words.
Use the following to display your excerpt. Remember to replace get_the_excerpt
with get_the_content
if you need an excerpt of more than 55, and replace <a href="'. esc_url( get_permalink() ) . '">' . ' …' . __( 'Read more »', 'pietergoosen' ) . '</a>
with any excerpt ending you need. My ending display a "read more" text with the name of the post.
function pietergoosen_custom_excerpts($limit) {
return wp_trim_words(get_the_excerpt(), $limit, '<a href="'. esc_url( get_permalink() ) . '">' . ' …' . __( 'Read more »', 'pietergoosen' ) . '</a>');
}
Now just use echo pietergoosen_custom_excerpts($limit);
anywhere in your templates where you need to display excerpts. Just change $limit
to the actual amount of words, for example echo pietergoosen_custom_excerpts(45);
to display 45 words
EDIT
Have a look at my answer on a custom excerpt as well
This is the function I use for controlling excerpt/content lengths, especially in situations where the end user won't likely remember to add the <!--more-->
tag, but the design of the theme requires is.
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
From there, you can change your excerpt code in your template files from:
<?php the_excerpt();?>
...to:
<?php echo excerpt(25);?>
where 25 is the number of characters you want displayed. When designing themes, I tend to stick with characters instead of words because the spacing is a little more consistent.
If you have custom excerpt, the "excrept_lenght" filter will not work for it. You can use this filter to trim custom excerpt.
function custom_excerpt_length($excerpt) {
if (has_excerpt()) {
$excerpt = wp_trim_words(get_the_excerpt(), apply_filters("excerpt_length", 30));
}
return $excerpt;
}
add_filter("the_excerpt", "custom_excerpt_length", 999);
Good Luck!
If you found this question, because you are using the "Excerpt" block in Gutenberg editor, and you are trying to set a default length for it (or anything other than the originally allowed 10-100 word value) you may find that the excerpt_length
filter has no effect on the block, and you can't even use the_excerpt
filter to override the excerpt's content. This is is because of the latest updates in the Excerpt block in WordPress.
However, you can use the following code to edit the default block settings of the Excerpt Gutenberg block:
function filter_metadata_registration( $metadata ) {
if ($metadata['name'] === 'core/post-excerpt') {
$metadata['attributes']['excerptLength'] = [
'type' => 'number',
'default' => 9999,
];
}
return $metadata;
};
add_filter( 'block_type_metadata', 'filter_metadata_registration' );
More on this: https://core.trac.wordpress.org/ticket/59348
本文标签: excerptexcerptlength not working
版权声明:本文标题:excerpt - excerpt_length not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736805705a1953677.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论