1

Here's my scenario:

  1. I'm using body_class() in my <body> tag as per the Codex instructions
  2. I've created specialized page templates (as described here) for the following pages:

    • Home: page-home.php
    • Services: page-services.php
    • About: page-about.php
    • Contact: page-contact.php
  3. The page templates themselves load just fine.

  4. The problem begins with the classes being returned by body_class(): for the first two pages it works fine, returning .page-template-page-home-php and .page-template-page-services-php respectively. For the other two pages it just returns .page-template-default, despite the fact that it is clearly not using the default page templates.

Does anyone know what might be causing this?

I know several ways of forcing it to add the classes, so I'm not looking for a workaround. I'm just wondering why this functionality doesn't seem to work correctly out-of-the-box.

UPDATE: Having poked around the body_class() function I've narrowed down the cause; it seems the page templates for those two pages hadn't been saved to the post meta table. The next question is what may have caused this.

  • 2
    the class .page-template-{name}-php appears when you've assigned a custom page template under the Page Attributes meta box, but your template names appear to be following the page-{slug}.php naming convention. Is it possible you are confusing the two types of page templates? – Milo Feb 13 '15 at 23:08
  • I think Milo has it. When editing the page, if "default template" is assigned, the template hierarchy will use the correct PHP file (slug in your case) but not display a unique class. – jdm2112 Feb 13 '15 at 23:30
  • @milo Unfortunately, no. I'm only using specialized page templates (i.e the page-{slug}.php convention you mentioned). I'm not using any custom page templates (i.e with the template name at the top of the file). Therefore I don't even have a Page Attributes meta box. As an aside, doesn't it make you wish WordPress would gives these clearer names? It is very possible to confuse the two types. – Dre Feb 14 '15 at 9:15
1

The comment from @milo prompted me to take a closer look, and I realised I was chasing a false positive. The About page and Contact page were actually displaying the correct behaviour; it was Home and Services that were wrong. It seems at some point those two pages did have custom page templates, which were later turned into specialized page templates. However, their body classes persisted.

The important thing to take away is: when a custom page template is removed/deleted and there are no other custom page templates to fallback to, the value is not cleared from the post meta, which means it will continue to show those body classes until a new template is assigned.

I will check if this is logged as an issue on the WP Trac later on today.

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.