Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer 1

up vote 2 down vote accepted

You're probably looking for something like this:

<?php

    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
add comment

Your Answer

 
discard

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.