6

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

2 Answers 2

18

Use this regex:

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.