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 trialvaleriuv
20,999 PointsIs 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?
1 Answer
Sean T. Unwin
28,690 PointsYou 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
20,999 PointsThanks, that seems to be what I was looking for.
Scott Paterson
26,869 PointsScott Paterson
26,869 PointsOne 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.