admin管理员组文章数量:1332872
Title says pretty much all of it. I need a filter that I can use to change excerpt content for those posts that have no excerpt set.
Now, I've tried both the_excerpt
and get_the_excerpt
, they both pass one parameter, and in both cases said parameter is empty string.
That said, I will need to hook onto a filter that has access to the auto-generated except, and lets me change it.
Title says pretty much all of it. I need a filter that I can use to change excerpt content for those posts that have no excerpt set.
Now, I've tried both the_excerpt
and get_the_excerpt
, they both pass one parameter, and in both cases said parameter is empty string.
That said, I will need to hook onto a filter that has access to the auto-generated except, and lets me change it.
Share Improve this question asked Jun 14, 2020 at 8:57 errorouserrorous 1691 silver badge9 bronze badges1 Answer
Reset to default 1The proper hooks for modifying the post excerpt are the ones you already tried: get_the_excerpt
and the_excerpt
, and WordPress actually uses the former one to generate an excerpt from the full post content, if there's no custom or manually-specified excerpt for the post — below is the relevant code in wp-includes/default-filters.php
:
add_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 );
So if you want to access the auto-generated excerpt, then with the the_excerpt
hook, you can do it like so:
add_filter( 'the_excerpt', function ( $excerpt ) {
return has_excerpt() ?
'Has custom excerpt: ' . $excerpt :
'Automatic excerpt: ' . $excerpt;
} );
But take note, the automatic excerpt may not necessarily be the one initially generated by WordPress and WordPress might not even be the one that generated the excerpt — plugins could have completely overridden it or just customized it, just as you can do the same.
And as you may have guessed it, you can remove the default WordPress filter and then use your own callback to generate your own "automatic" excerpt:
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 );
add_filter( 'get_the_excerpt', function ( $excerpt, $post ) {
return $post->post_excerpt ?
'Has custom excerpt: ' . $excerpt :
'Here, create your own excerpt.';
}, 10, 2 );
本文标签: postsHook for changing excerpt content when excerpt not set
版权声明:本文标题:posts - Hook for changing excerpt content when excerpt not set 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742347909a2457899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论