admin管理员组文章数量:1200973
I use a theme that calls get_the_content()
to display short excerpts of the latest blog posts on the home page. Now I noticed that the excerpt sometimes starts with an image caption, if the blog post contains a picture at the very beginning. I usually do that and make the text float around it.
I always set the first three caption fields on an image (Alt Text, Title, Caption).
Is there any way to strip these image captions from the return value of get_the_content()
or is there any other method that I could possibly call instead? I checked the documentation, but there seems no argument to exclude images from the return value.
I use a theme that calls get_the_content()
to display short excerpts of the latest blog posts on the home page. Now I noticed that the excerpt sometimes starts with an image caption, if the blog post contains a picture at the very beginning. I usually do that and make the text float around it.
I always set the first three caption fields on an image (Alt Text, Title, Caption).
Is there any way to strip these image captions from the return value of get_the_content()
or is there any other method that I could possibly call instead? I checked the documentation, but there seems no argument to exclude images from the return value.
1 Answer
Reset to default 3It turned out that the method called strip_tags(preg_replace(" (\[.*?\])", '', $output))
before strip_shortcodes($output)
, which caused aforementioned issue, since the code removed shortcode in square brackets, but retained contained image captions.
I could fix it by swapping the two method calls like this:
$output = get_the_content();
$output = strip_shortcodes($output); // Strip WordPress shortcodes first!
$output = strip_tags(preg_replace(" (\[.*?\])", '', $output));
本文标签: the contentOmit image captions from getthecontent()
版权声明:本文标题:the content - Omit image captions from get_the_content() 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738565570a2100077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
strip_tags()
andstrip_shortcodes()
are applied to the excerpt already, but the image caption is plain text. There is no CSS identifier either. Is there no alternative toget_the_content()
? – user1438038 Commented Jan 18, 2020 at 13:23$output = get_the_content();
– user1438038 Commented Jan 19, 2020 at 12:25