Excerpt first paragraph

Would it be possible to take the first paragraph in an excerpt() instead of word or character? We want to give the first paragraph a different look. I realize css :first-of-type may be the easiest solution here.

If it’s just a matter of styling, CSS is definitely the way to go. Of course, you could also use some regex voodoo to get the first paragraph, add a class to it and then style, but I don’t think this will be necessary.

For kicks, if I wanted the voodoo, would I somehow extend the excerpt() method?

You can’t really extend the field method. The excerpt field method internally uses the excerpt() helper which in turn uses the excerpt() method of the string class.

You can create your own custom field method if you want. But why if you can achieve the same with less code/overhead unless you want to adhere to some BEM scheme? Or want to wrap the first paragraph in some container?

Here is a function I used in the past :

<?php 
   function getFirstPara($string){
        $string = substr($string,0, strpos($string, "</p>")+4);
        return $string;
    } 

1 Like

You’re right, CSS seems like the right approach here. I’m glad to have your code snippet in case I need to wrap in a div someday. Thank you!