Strip shortcode, keep content in between

烂漫一生 提交于 2020-06-08 07:52:12

问题


The idea behind this question lies in getting excerpt of the posts created with avada but I can't strip the shortcodes from the post content to display the excerpt of the post.

Here is example of my post (using avada):

[fullwidth background_color="" background_image="" class="" id=""]
[one_full last="yes" spacing="yes" class="" id=""][fusion_text]
Content text ...   
[/fusion_text][/one_full][/fullwidth]`

The default the_excerpt(); doesn't work because of shortcodes. get_content() returns the full post content including shortcodes. Using strip_shortcodes() also removes the content between the shortcodes.

So my plan would be to strip shortcodes using pattern? and trim the message to mimic the excerpt functionality. PS: This pattern doesn't work.


回答1:


Use this regex:

$excerpt = get_the_excerpt();
$excerpt = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $excerpt);



回答2:


It's worth commenting that you can do this same thing using a text editor, for instance Notepad++ or EditPad Pro, using this Regex: \[/?[^/\]]+/?\]

That will match all shortcode, and you can then replace with nothing or a space, whatever.



来源:https://stackoverflow.com/questions/33133953/strip-shortcode-keep-content-in-between

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!