• Resolved derpaa

    (@derpaa)


    Hi, I am trying my hand at making my first custom page template and I am new to php. My understanding is that I can use the body_class function to change the background image and color of the overall page for this specific template, but I am having trouble using it.

    I put <body <?php body_class(); ?>> as the body tag in the header.php file, but I don’t understand where and how in the TEMPLATE file I set which body class it should use, or how that gets passed to the header file.

    The tutorials I’m reading say that you pass your custom class as a function argument to body_class(), as in:

    <body <?php body_class(‘my-class’); ?>>
    But this doesn’t seem like it would work since the header file is for EVERY page. Can I set the body class id in the template and get it to pass to the header’s body tag somehow, or how do you do this?

    Thank you for any assistance you can provide!

Viewing 2 replies - 1 through 2 (of 2 total)
  • It isn’t technically required to pass an argument to body_class() for this purpose. When you use custom page template “foo.php”, you’ll have a class named page-template-foo-php on your <body> tag, which you can target with CSS normally:

    body.page-template-foo-php { background: url(path/to/image); }

    Thread Starter derpaa

    (@derpaa)

    Ahhh, thank you very much!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Help using body_class()’ is closed to new replies.