0

I've got a template called News.

When I go to the Dashboard and edit the Page called News which has a web address of news.html I can see it uses this template (i.e. in the Page Attributes > Template section).

However, if I then update the Template Name (i.e. in page-news.php) to something like NewsTest and refresh the News Page in the Dashboard I see that WordPress has updated the Template being used in the Page Attributes > Template section.

Here's another more specific example:

If I then edit the slug to newsaaa and the template name to Newsbbb it still shows up in the Page Attributes > Template section.

I have been working my way through the Template Hierarchy but am still a bit puzzled how exactly WordPress knows which file it should use for that Page.

Can anyone shed some light?

3
  • As far as I know, WordPress doesn't connect the page with the template by the templates name but by the templates file name. Commented Apr 13, 2015 at 14:21
  • 2
    Please improve your question title. Summarize your specific problem, do not include the word WordPress.
    – fuxia
    Commented Apr 13, 2015 at 14:28
  • @websupporter yes, you're right. that confused me initially.
    – Snowcrash
    Commented Apr 13, 2015 at 14:48

2 Answers 2

2

You need to look how page templates are saved against a page in the wp_postmeta table. Each page is linked to the specific template it uses and this is saved under the meta key _wp_page_template. You can go and have a look at your own install how you db structure looks like and how page templates are save

Wordpress does not use page slugs or page names but page id's to create the relationship to the page template assigned to it. Also, Wordpress does not use the template name to create the relationship to the page, but the page template file name. In the case of the default template, page.php, it is simply saved as default and not the file name

For that reason, you can rename the page slug and the template name to whatever you like and still have the relationship between the specific page and the template it uses.

Example:

enter image description here

3

The template name is defined inside the file, this way:

<?php
/*
Template Name: My Custom Page
*/

This page will tell you all : https://codex.wordpress.org/Page_Templates

1
  • Sebastien has the right idea, but doesn't explain why this is the answer. ... From what it sounds like you have a page-template called "News" (which the name is defined in the comment section, Sebastien points out, at the top of any page template file.) and the file name of this template is what you've been changing. WordPress uses the name in the comment section (see above) to display in the drop down when you are editing a page. So as you changed the file name, WP doesn't care and still had the News template selected because you didn't change the name in the drop down. Does that make sense?
    – Sean Grant
    Commented Apr 13, 2015 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.