Grab the first paragraph of each post

You can use this function:

function get_first_paragraph(){
    global $post;
    $str = wpautop( get_the_content() );
    $str = substr( $str, 0, strpos( $str, '</p>' ) + 4 );
    $str = strip_tags($str, '<a><strong><em>');
    return '<p>' . $str . '</p>';
}

and then call it in your loop with:

<?php echo get_first_paragraph(); ?>

The magic part your looking for is wpautop, a WordPress function, which will convert double line-breaks in the text to proper paragraphs.

With wpautop in place, you can then use the PHP function substr to get the first paragraph starting at the first character until it reaches the first closing paragraph, and then add 4 characters so that the closing tag is not stripped.


To further expand on this, if you want to then get everything except for the first paragraph you can use this complementary function which will start at the end of the first closing paragraph tag and get everything after it:

function get_the_post(){
    global $post;
    $str = wpautop( get_the_content() );
    $str = substr( $str, (strpos( $str, '</p>')));
    return $str;
}

and call it in the loop with:

<?php echo get_the_post(); ?>

Leave a Comment