Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using this function to limit the content in my themes. But the problem is whenever I call the function, it also displays the image caption. I want to remove the image caption when calling the_content_limit function.

Here is the code:

function the_content_limit($max_char, $more_link_text = '', $stripteaser = 0, $more_file = '') {

    $content = get_the_content($more_link_text, $stripteaser, $more_file);

    $content = apply_filters('the_content', $content);

    $content = str_replace(']]>', ']]>', $content);

    $content = strip_tags($content);



   if (strlen($_GET['p']) > 0) {

      echo "";

      echo $content;

      echo "&nbsp;<a href='";

      the_permalink();

      echo "'>"."Read More &rarr;</a>";

      echo "";

   }

   else if ((strlen($content)>$max_char) && ($espacio = strpos($content, " ", $max_char ))) {

        $content = substr($content, 0, $espacio);

        $content = $content;

        echo "";

        echo $content;

        echo "...";

        echo "&nbsp;<a href='";

        the_permalink();

        echo "'>"."</a>";

        echo "";

   }

   else {

      echo "";

      echo $content;

      echo "&nbsp;<a href='";

      the_permalink();

      echo "'>"."Read More &rarr;</a>";

      echo "";

   }

}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Image captions in Wordpress are actually shortcodes. Shortcodes are applied by the filter:

$content = apply_filters('the_content', $content);

For example, Wordpress creates the following code in your content when you enter an image caption:

[caption id="attachment_55" align="alignleft" width="127" caption="Here is my caption"][/caption]

You need to still use apply_filters() in order to properly display content. (safe content display and all other shortcodes)

If you don't want shortcodes (which is what it looks like, since you are doing a striptags) you should just use this:

 $content = strip_shortcodes( $content );

But if it is specifically [caption] shortcodes, I assume this could work, if you just want to add a string-replace line to your code:

$content = get_the_content($more_link_text, $stripteaser, $more_file);
// remove [caption] shortcode
$content = preg_replace("/\[caption.*\[\/caption\]/", '', $content);
// short codes are applied
$content = apply_filters('the_content', $content);
share|improve this answer
    
Thank you for your quick solution. That's all I wanted to do. –  pervez Oct 30 '12 at 10:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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