• Resolved echofoxtrot

    (@echofoxtrot)


    I read in the Page Templates section of the Codex that:
    “Note: A specialized template (page-{slug}.php or page-{ID}.php) also gets the page-template-default class rather than its own body class.”

    However, when I checked the body class of my custom page template, it did show its own body class rather than page-template-default. That is, my page template called “page-klingon.php” shows a body class of “page-template-page-klingon page-template-page-klingon-php” (among other classes), but not page-template-default.

    Site: https://angelxray.com

    I am trying to understand why that would be. Any help is greatly appreciated. Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • What you’re seeing is correct behavior, it’s just that the Codex is a bit awkwardly worded on this point. When the Codex refers to a “specialized template”, it refers to a file page-121.php, which would be used only for a static page with the ID 121, or page-contact.php, which would be used only for a static page with the slug “contact”. Those kinds of specialized templates are good if you only want one of your pages to be different. You’d use page templates like you’re using if you’d like multiple pages to share the same look; it would be unwieldy to have page-121.php, page-345.php, page-1011.php, etc.

    Thread Starter echofoxtrot

    (@echofoxtrot)

    Thanks for the fast response, stephencottontail! Well, I did read that part, but it turns out that my template “page-klingon.php” can be used on any page, not just the Klingon page, if you select it from the templates list. This is my understanding in general; please let me know whether I have it right:
    – If you make a template with the form page-slug.php (page-klingon.php, for example), and the slug of the relevant page is “klingon”, then if you go to that page, it will use the page-klingon.php template even if you do not select that template from the Page Attributes/Templates list. WordPress will match the slug to the part of the template name following “page”, such as “-klingon.” This would be true even if the selected page template is Default.
    – However, if you have a page whose slug is other than “klingon,”, you could still use the template page-klingon.php for that page by selecting the klingon page template from the list of templates. That is how my site is behaving.

    Is what I am describing accurate? Thanks again for your help!

    Note: For clarity, throughout this post, when I refer to “specialized templates”, I’m referring to files with the name page-121.php or page-contact.php, and when I refer to “page templates”, I’m referring to files with Template Name: Contact in the header.

    You’ve got it right. This is a bit of an oddity with the specialized template and the page template system, which is why it’s not recommended to name your page templates as specialized templates. If you have a file named page-klingon.php with Template Name: Klingon in the header, WordPress interprets it as both a page template (because of the header) and a specialized template (because of the file name). This can lead to some unexpected behavior.

    Thread Starter echofoxtrot

    (@echofoxtrot)

    Aha. Got it. That is exactly what I did – the file name is page-klingon.php with the Template Name: Klingon Page.

    I just now made a new specialized template called page-romulan.php, but did not include any Template Name: at the top. I then named a new WordPress page “Romulan”, and it has the slug “romulan.” That page does use the page-romulan template, even though it could not be selected, and its body class is the default page.

    You’re right – the Codex is a little confusing with this, but you explained it. Thank you very much, stephencottontail! I appreciate it!

    And after all that, I am not even a Trekkie! ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Body class on custom page template is name of custom template, not default’ is closed to new replies.