Last Updated: February 25, 2016
·
1.452K
· danhodkinson

Different style first paragraph on specific pages, wordpress

Creating a bolder, larger first paragraph is common amongst sites these days.
I created a bit of code that allowed me add a class to the first paragraph. The only problem was this was site wide and there were a few pages where i didn't want this.

So, in my funcitons.php i added this code:

<?php function first_paragraph($content){
   return preg_replace('/<p([^>]+)?>/', '<p$1 class="intro">', $content, 1);
 }?>

and then on every page/template that i did want it to appear i added the following code, just inside the loop:

<?php add_filter('the_content', 'first_paragraph');?>

if you did want it to apply to every page, then you would simply add the add_filter to the functions.php instead.

enjoy

Related protips