posts - Remove Shortcode [...] from Blog Preview

admin2025-06-03  4

When utilizing shortcode (plugin, etc.) near the top of the page, the plugin shortcode displays in the preview. Is there a way to hide text within brackets [text like this] from a preview on a recent posts type of page?

The following example shows the shortcode within a blog post preview:

When utilizing shortcode (plugin, etc.) near the top of the page, the plugin shortcode displays in the preview. Is there a way to hide text within brackets [text like this] from a preview on a recent posts type of page?

The following example shows the shortcode within a blog post preview:

Share Improve this question asked Oct 15, 2015 at 16:50 beta208beta208 1911 silver badge13 bronze badges 3
  • 1 If you don't want shotcodes in excerpt then why not to just write one? – Mark Kaplun Commented Oct 15, 2015 at 17:12
  • Interesting, to manually replace the excerpt, when editing the blog post where do I go to write that? I do not have it as an option. – beta208 Commented Oct 15, 2015 at 17:14
  • Thank you, that was correct. I simply needed to enable 'Screen Options > Excerpt'. – beta208 Commented Oct 15, 2015 at 17:15
Add a comment  | 

4 Answers 4

Reset to default 2

You can do with PHP. Just remove part where is get_content() and add this:

<?php 
            $content=get_the_content();
            $content = preg_replace('#\[[^\]]+\]#', '',$content);
            echo apply_filters('the_content', $content);
        ?>

That is regular expression added inside content. This regex will remove all tags inside content.

Use this instead if you don't wanna manually write excerpts every time:

function wpse205632_filter_excerpt( $excerpt ) {

    $excerpt = strip_shortcodes( $excerpt );

    return $excerpt;
}
add_filter( 'get_the_excerpt', 'wpse205632_filter_excerpt' );  

Just add this snippet in functions.php and you are good to go.

Excerpt was not showing but would do the trick. On the edit post page, accessing 'Screen Options' and selecting 'Excerpt' allows one to manually fill the excerpt.

This is what I used to get content as excerpt with limited amount of words, and exclude shortcodes from Visual Composer

<?php $content=get_the_content(); $content = preg_replace('#\[[^\]]+\]#', '',$trimmed_content = wp_trim_words($content, 20)); echo apply_filters('the_content', $content, $trimmed_content); ?
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748941418a315013.html

最新回复(0)