WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm building a template for twentyten child theme. I need to add a class in in the template page, but not in other pages.My template inherits the header and footer, so, I can't change any markup there. How can I add a class in there without adding that class to other pages? Thanks a lot for the help.

share|improve this question
twenty ten uses body_class - so each page has already a unique css class. what are you trying to achieve that would require a specific css class? – Michael May 28 '11 at 20:13
I'm actually trying to add top margin. and my template isn't echoing any class, just <div id="main">. It inherits everything from it's parent theme except the sidebar, which is different. – Samia Ruponti May 28 '11 at 20:22
the page specific class is added to the body tag; for special styling, use something like this in style.css .page-template-yourtemplatename-php #main { ... } – Michael May 28 '11 at 21:09
thanks Michael!!that worked perfectly!!! – Samia Ruponti May 29 '11 at 5:31
sorry, I wanted to mark your comment as great, but I can't find the arrow on the side as stackoverflow has! – Samia Ruponti May 29 '11 at 5:43
up vote 2 down vote accepted

You're probably looking for something like this:


    function add_classes_to_body_class($classes) {

            // make sure 'page-template.php' matches your template name
            if ( is_page_template('page-template.php') ) {
                $classes[] = 'main';
        return $classes;
    add_filter( 'body_class', 'add_classes_to_body_class' );


make sure you add body_class() into your markup. Traditionally it would be in your body tag like so. <body id="foobar" <?php body_class(); ?>>

share|improve this answer
thank you for the code. I'll have to give it a try!!! – Samia Ruponti May 29 '11 at 5:33
@Snowbell92 no problem, glad to help out. – VicePrez May 29 '11 at 5:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.