0

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);
//else
// After the content
$wpse0001_excerpt .= $excerpt_end;

return $wpse0001_excerpt;

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

endif; 

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:

<blockquote>
 <p>Text</p>
 <p>Text</p>
</blockquote>

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

<blockquote>
 <p>Text</p></p>
</blockquote>

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

3
  • 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 Commented Aug 19, 2014 at 15:28
  • @PieterGoosen thank You but I really need to get first two paragraphs :)
    – SLH
    Commented Aug 19, 2014 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
    Commented Aug 19, 2014 at 15:44

1 Answer 1

0

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.

4
  • 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
    Commented Aug 19, 2014 at 15:24
  • User @Lix from stackoverflow gave me a solution. $excerpt = preg_replace( '/<\/p>\s+<\/p>/', '</p>', $excerpt );
    – SLH
    Commented Aug 19, 2014 at 15:48
  • @RafcioKowalsky can you link to that post Commented Aug 19, 2014 at 15:54
  • stackoverflow.com/a/25387705/1786842
    – SLH
    Commented Aug 19, 2014 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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