Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

valeriuv
valeriuv
20,999 Points

Is there an easy way to add a CSS class to certain pages in Wordpress?

I would like to have the same styles applied only to certain Wordpress pages. Is there an easy way to add these classes to those pages?

Scott Paterson
Scott Paterson
26,869 Points

One way that springs to my mind is add the body class function <?php body_class(); ?> in side your body tag and then when you inspect your site through your Google inspect element check your body tag and what classes that shows up, and target your pages that way.

1 Answer

Sean T. Unwin
Sean T. Unwin
28,690 Points

You could add a filter to 'body_class()' in your functions.php.

E.g.

add_filter( 'body_class', 'valeriuv_custom_body_class');
function valeriuv_custom_body_class( $classes ) {
     if ( is_page(array('mypage', 'myotherpage-slug') )
          $classes[] = 'css-class-name';

     return $classes; 
}

Resources

valeriuv
valeriuv
20,999 Points

Thanks, that seems to be what I was looking for.