Add Class to Specific Paragraph of the_content()

Let’s say I want to give the first paragraph bold text by adding a body class to the first paragraph. Is there a way to filter the output of


or any other paragraph? First paragraph? Last paragraph?

I know I can do this using Javascript or CSS (:firstchild), but is there a native WordPress way?




Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The following bit of code adds a class to the first paragraph output by the_content:

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

Add the above to your theme’s functions.php file.

Then in your CSS add something like:

p.intro { font-weight:bold; }

I can’t claim credit for this solution (see this thread in the WP forums) but I tested it and it worked great for me in WordPress 3.3.2. You should be able to modify it as needed to target whichever paragraph your heart desires.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments