Grab the first paragraph of each post

I have some code that uses preg_match to grab the first paragraph, and then spit it out in the loop:

function first_paragraph() {
  global $post, $posts;
  $first_para = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('%(<p[^>]*>.*?</p>)%i', $post->post_content, $matches);
  $first_para = $matches [1] [0];
  echo $first_para;
}

However, there is a little problem with this. It only works when I manually wrap text in <p> </p> tags in the editor, and not otherwise. Regular expressions are certainly not my forté, so any help or further understanding would be great.

Read More

And this works, also wraps the images/iframes in p tags:

function first_paragraph() {
  global $post, $posts;
  $first_para = '';
  ob_start();
  ob_end_clean();
  $post_content = $post->post_content;
  $post_content = apply_filters('the_content', $post_content);
  $output = preg_match_all('%(<p[^>]*>.*?</p>)%i', $post_content, $matches);
  $first_para = $matches [1] [0];
  echo $first_para;
}

Related posts

2 comments

  1. 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(); ?>
    
  2. Try this:

    function first_paragraph() {
        global $post, $posts;
        $post_content = $post->post_content;
        $post_content = apply_filters('the_content', $post_content);
        $post_content = str_replace('</p>', '', $post_content);
        $paras = explode('<p>', $post_content);
        array_shift($paras);
    
        return $paras[0]; 
    }
    

Comments are closed.