Remove image captions / shortcode from main news page (5 posts)

  1. hamsterpants
    Posted 3 years ago #

    Hello - I have a custom wordpress website and our news page contains a list of all our 'posts' by way of teaser text (approx the first 20 words).

    Some of these posts contain images with captions - however the caption code appears in the teaser text on the main news page!

    I have spent several hours trying to find a way to remove these - but none seem to work.

    The closest I got was

    add_filter('the_content', 'strip_shortcodes');
    function remove_shortcode_from_index($content) {
      if ( is_search() ) {
        $content = strip_shortcodes( $content );
      return $content;

    But this only works on the posts, not on the main news index. And even then it removes the entire image rather than just the caption.

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    Use the_excerpt() on the page - not the_content().

  3. hamsterpants
    Posted 3 years ago #

    We don't want to have to manually write excerpts each time if possible - there are thousands of posts and this adds an extra time consuming step

  4. esmi
    Forum Moderator
    Posted 3 years ago #

    You don't have to. the_excerpt creates an automatic excerpt. If you want to reduce the default excerpt output from 55 words to 20, use something like:

    // Decrease excerpt length
    function my_excerpt_length($length) {
    	return 20; // Or whatever you want the length to be.
    add_filter('excerpt_length', 'my_excerpt_length');
  5. blackriver
    Posted 3 years ago #

    Another new handy function which might help in similar situation wp_trim_words()

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.