0

I am working on a legacy WordPress application, and I came across a few files with the naming template-{slug}.php, which were placed in the child-theme folder. I'm surprised I came up with no support via Google when I looked into this. I see lots of support about page-{slug}.php files, but that seems to be a simpler concept which I understand easily.

It's clear to me that the template-{slug}.php files control the layout of pages that are set to use the corresponding template, which I can select in the Edit Page area. But on other WordPress applications that I'm working on, I don't see any choice for a template for pages when I edit them.

So my question is, should I assume that this is a theme-based functionality, where some themes provide support for files with the template-{slug}.php naming? I'm using Sparkling Theme.

Or do all WordPress applications allow for such files? If so, where can I find instructions on how to use template-{slug}.php files? Or is there another way these days to create a true "template" where multiple pages can use its layout?

5

So my question is, should I assume that this is a theme-based functionality, where some themes provide support for files with the template-{slug}.php naming?

Correct.

Or do all WordPress applications allow for such files? If so, where can I find instructions on how to use template-{slug}.php files? Or is there another way these days to create a true "template" where multiple pages can use its layout?

I don't really understand what you mean by "all WordPress applications". In WordPress, templates are provided by the theme. So if you want a custom template that can be used for pages, it needs to be part of the theme. The documentation for how page templates work, including how custom selectable templates work, is here.

If you're using a child theme, you'd just create the template you want, probably basing it off an existing template, and include it in the child theme. One thing that you'll notice from the documentation, that I've linked above, is that page-{slug}.php has a specific meaning that allows the template to be used automatically. template-{slug}.php does not have any such meaning. It's merely a convention. What makes a template selectable is the presence of the Template Name comment at the beginning of the file:

<?php
/**
 * Template Name: Full Width Page
 */
  • "which I can select in the Edit Page area. But on other WordPress applications that I'm working on, I don't see any choice for a template for pages when I edit them" -> wouldn't this speak against the use of regular templates via Template Name: header and rather for some kind of other (theme related) magic? – kero Oct 16 at 15:59
  • 1
    Not necessarily, it just means that some (many?) themes don't have additional template options. Not every theme requires it. If you're doing something bespoke, then whether or not template options are appropriate would depend on the needs of the site. – Jacob Peattie Oct 16 at 16:02
  • This is great. On a separate note, I've always been curious how WordPress knows to use those kind of comments (such as Template Name) at the beginning of the file; I wonder why they get special functionality and aren't just treated as comments. I suppose that's a separate topic. – Mark Oct 16 at 16:15
  • @JacobPeattie Do I have to wait for a certain time period for the template files to be rescanned? I built a small file in my child theme folder and put the Template Name in comments at the top, but I don't yet have an option to select this template on my pages. – Mark Oct 16 at 16:33
  • 1
    No, there’s no wait. Make sure the template is no more than 1 level deep in your theme, and does not use a file name reserved for other templates (see the documentation). Also make sure you’re editing a page, not a post. – Jacob Peattie Oct 16 at 16:38

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.