So im trying to display a certain amount of blog post content within a search and filter query for my clients site. The issue im running into however is that when I use this line of code:
$content = get_the_content();
$trimmed_content = wp_trim_words( $content, 50, NULL );
echo $trimmed_content;
The content that is displayed shows enfolds markup and shortcodes.
When I use the_content instead the page appears shrunken and wonky.
What do I need to add in order for the shortcodes to not appear when printed on the front end?
Hey Andrea,
You can remove the shortcodes by using https://codex.wordpress.org/Function_Reference/strip_shortcodes
Best regards,
Basilis
Hi Basilis,
I have tried the strip_shortcodes hook as well without success.
Here’s what I tried:
$content = get_the_content();
$stripped_content = strip_shortcodes( $content );
echo $stripped_content;
When I do this the posts display absolutely nothing. When I try the_content instead the result shows the page looking very unpleasant.
Hi,
Are you doing this inside a post loop? If not, you may need to add the id of the post inside the get_the_content function. Or try to use the preg_replace function.
$content = get_the_content();
$content = preg_replace('#\[[^\]]+\]#', '',$content);
echo $content;
Thank you for the update.
Best regards,
Ismael
Ismael,
This isn’t exactly what I would like to show (because it doesn’t show the styles ALB offers) but we will make this work.
Thanks for letting me know about preg_replace!