JRoy
7/2/2018 - 3:00 PM

Limit Content to 2 Paragraphs

<?php 

$full_content = get_the_content();
$full_content = preg_replace("/<img[^>]+\>/i", " ", $full_content);          
$full_content = apply_filters('the_content', $full_content);
$full_content = str_replace(']]>', ']]>', $full_content);

$numMatches = preg_match_all('|<p>(.+?)</p>|', $full_content, $matches);

$trimmed_content = "";
$count = 1;
$one_paragraph = false;
foreach($matches[1] as $paragraph){
    if($count == 1){
        if( strlen($paragraph) < 440){
            $trimmed_content .= "<p>" . $paragraph . "</p>";
        }else{
            $trimmed_content .= "<p>" . $paragraph . "</p>";
            $one_paragraph = true;
        }
    }
    
    if($count == 2 && $one_paragraph == false){
        $trimmed_content .= "<p>" . $paragraph . "</p>";
    }
    $count++;
}