I'm using the following code from this question in order to limit the excerpt length to 2 paragraphs.

if ( ! function_exists( 'wpse0001_custom_wp_trim_excerpt' ) ) : 

function wpse0001_custom_wp_trim_excerpt($wpse0001_excerpt) {
global $post;
$raw_excerpt = $wpse0001_excerpt;
if ( '' == $wpse0001_excerpt ) {

$wpse0001_excerpt = get_the_content('');
$wpse0001_excerpt = strip_shortcodes( $wpse0001_excerpt );
$wpse0001_excerpt = apply_filters('the_content', $wpse0001_excerpt);
// Here we choose how many paragraphs do we want to cutthe excerpt at, This part thanks to Clément Malet
$wpse0001_excerpt = "<p>$wpse0001_excerpt</p>";
    $wanted_number_of_paragraph = 1;
    $tmp = explode ('</p>', $wpse0001_excerpt);
    for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
       if (isset($tmp[$i]) && $tmp[$i] != '') {
           $tmp_to_add[$i] = $tmp[$i];
$wpse0001_excerpt = implode('</p>', $tmp_to_add) . '</p>';

$wpse0001_excerpt = str_replace(']]>', ']]&gt;', $wpse0001_excerpt);

$excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', 'pietergoosen' ), get_the_title()) . '</a>'; 
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end); 

//$pos = strrpos($wpse0001_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse0001_excerpt = substr_replace($wpse0001_excerpt, $excerpt_end, $pos, 0);
// After the content
$wpse0001_excerpt .= $excerpt_end;

return $wpse0001_excerpt;

return apply_filters('wpse0001_custom_wp_trim_excerpt', $wpse0001_excerpt, $raw_excerpt);


remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse0001_custom_wp_trim_excerpt');

I have set $wanted_number_of_paragraph to 2.

If found more than one paragraph in a post the result is correct:


If found only one paragraph in a post the result is incorrect:


I tried to modify a bit the above code but none of my modifications works. Any ideas why this happens?

  • I have rolled back my last change on the code as it seems that that piece of code is troublesome. Will work on the code again. The current code will only display the first paragraph. Check it out – Pieter Goosen Aug 19 '14 at 15:28
  • @PieterGoosen thank You but I really need to get first two paragraphs :) – SLH Aug 19 '14 at 15:32
  • Instead of catting </p> at the end of the loop after you implode the array again, why not include it inside the loop: $tmp_to_add[$i] = $tmp[$i] . '</p>'? Then I think you can get away with imploding on an empty string. – andrewsi Aug 19 '14 at 15:44

It happens because using explode() to parse HTML is at least equally evil as trying to do it with regex.

To “fix” it, the quick ’n dirty way, you could add the following preg_replace() after the implode() line:

$wpse0001_excerpt = implode( '</p>', $tmp_to_add ).'</p>';
$wpse0001_excerpt = preg_replace( '/<\/p>\s*<\/p>/', '</p>', $wpse0001_excerpt );

Using something like PHP’s DOM extension would be cleaner. This is no longer WordPress related, though.

| improve this answer | |
  • Thank you for your effort but this is not working, because there are a lot of spaces in generated HTML (by PHP) between first /p and second /p... I tried 'almost' everything and I'm stuck with that ;/ – SLH Aug 19 '14 at 15:24
  • User @Lix from stackoverflow gave me a solution. $excerpt = preg_replace( '/<\/p>\s+<\/p>/', '</p>', $excerpt ); – SLH Aug 19 '14 at 15:48
  • @RafcioKowalsky can you link to that post – Pieter Goosen Aug 19 '14 at 15:54
  • stackoverflow.com/a/25387705/1786842 – SLH Aug 19 '14 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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