Creating Custom Excerpts based on paragraphs

With a few of my clients, they wanted more than the standard excerpt. They wanted to display a paragraph or two of the post. without echoing out a whole post in an archive or landing page. Now WordPress has great examples on how to do things similar to this with get_the_excerpt(); and the_excerpt(); .However i wanted to try a new method, using a mix of stand PHP functions and a few WordPress functions I haven’t used before.

 function coocerpt(){
 	$content = get_the_content();
	$content = wpautop($content);
 	$link = get_the_permalink();
 	
 	 $post_content = $content;
 	$end = strpos($post_content, '</p>');
 	
 	$coocerpt = substr($post_content, 0 , $end);
 	$length = strlen($coocerpt);
 	$coocerpt .= "...";
 	$coocerpt .= " </p> <a href=" .  $link . " class='read-more'>Read More <i class='fa fa-arrow-right'></i></a>";

   return $coocerpt;
 
 }

First we get an unformatted copy of the content with get_the_content(). After this we run our variable through the function wpautop. This WordPress function takes whatever content is provided and opens a paragraph tag at the beginning. It then inserts paragraph tags at every double line break. This function is used in the_content().

Next, the function uses the String Position function and searches for the first instance of the closing paragraph tag. We store that string position in the $end variable for use next. Now that the end is set, we create coocerpt variable. Using the Substring function, we tell the function to search the content, and create a new substring from position 0, tlil whatever number the $end variable has stored.

Now that we have our first paragraph separated out, I appended a hellip (…) to indicate that its not the whole article and added on my own read more link.

There you have it! Did i explain something wrong or do you have a better way? Comment below and point it out.