问题
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