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.

  • 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
  • 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

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(); ?>>

| improve this answer | |
  • @Snowbell92 no problem, glad to help out. – VicePrez May 29 '11 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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