0

I'm trying to trim the excerpt to only the first paragraph by adding filters to my theme's functions.php file. I have this working fine on my local machine (using WAMP), but on my actually web server it outputs the entire post rather than the paragraph I'm expecting. Here's what I have to trim the excerpt:

// Set the excerpt length to more than necessary
function triangle_x_excerpt_length($length)
{
    return 999;
}
add_filter('excerpt_length', 'triangle_x_excerpt_length', 999);

// Get the first paragraph of the article
function triangle_x_excerpt($text, $raw_excerpt)
{
    if(!$raw_excerpt)
    {
        $content = apply_filters( 'the_content', get_the_content() );
        $text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
    }
    $text = preg_replace("/<img[^>]+\>/i", "", $text); 
    return $text;
}
add_filter('get_the_excerpt', 'triangle_x_excerpt', 20, 2);

// Remove read more link at end of excerpt
function new_excerpt_more($more)
{
    return '';
}
add_filter('excerpt_more', 'new_excerpt_more');

1 Answer 1

0

You can match this with regEx and easily choose the amount of paragraphs to return.

// in context
function triangle_x_excerpt( $text, $raw_excerpt ) {

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

    $text    = ( preg_match( sprintf( '~(<p>.+?</p>){%d}~i', 1 ), $content, $matches ) ) ? $matches[ 0 ] : $content;

    return preg_replace( "/<img[^>]+\>/i", "", $text );
}

$text = prefix_get_p( $content, 1);

function prefix_get_p($content, $max = 1)
{
    $max = is_numeric($max) ? absint($max) : 1;

    return (preg_match(sprintf('~(<p>.+?</p>){%d}~i', $max), $content, $matches)) ? $matches[0] : $content;
}

echo "Limit 1" . PHP_EOL;
echo prefix_get_p('<p>aaaaa</p><p>bbbbb</p><p>ccccc</p><p>dddddd</p><p>eeeeeee</p><p>ffffff</p>', 1); // <p>aaaaa</p>

echo "Limit 3" . PHP_EOL;
echo prefix_get_p('<p>aaaaa</p><p>bbbbb</p><p>ccccc</p><p>dddddd</p><p>eeeeeee</p><p>ffffff</p>', 3); // <p>aaaaa</p><p>bbbbb</p><p>ccccc</p>

echo "Limit 2" . PHP_EOL;
echo prefix_get_p('<p>aaaaa</p><p>bbbbb</p><p>ccccc</p><p>dddddd</p><p>eeeeeee</p><p>ffffff</p>', 2); // <p>aaaaa</p><p>bbbbb</p>
11
  • That still gives me the entire post content
    – Mike
    Commented Nov 23, 2016 at 4:41
  • Can you provide a sample of your content to test with?
    – jgraup
    Commented Nov 23, 2016 at 4:42
  • It would be a little difficult to do that with character limit on posts here... It has to work with any post though so anything that has multiple paragraphs will work for testing.
    – Mike
    Commented Nov 23, 2016 at 4:47
  • Could you edit you post to show exactly how I should edit my function, without the prefix_get_p function? That's making things more complicated than necessary and I'm getting a bit confused with it.
    – Mike
    Commented Nov 23, 2016 at 4:51
  • I've updated your filter and stripped out the function.
    – jgraup
    Commented Nov 23, 2016 at 4:54

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.