Template page-slug not working
-
I decided to write about a problem (instead of ignoring it and moving on) I recently encountered while developing a child theme using the Thematic Framework, hoping someone could shed some light on a possible cause and to prevent others wasting hours finding a solution like i did, ok here goes.
I had a page called Contact, created in the WordPress Admin as per usual, it was set to template ‘Default’. In my child theme directory I created a new template file (using the page-{slug}.php format) called page-contact.php. Ok heres the problem, the Contact page would not use the page-contact.php template file, no matter what I did it just wouldn’t use this template.
Let me describe in a little more detail my site. Very simply it consisted of a Home, About, Showcase and Contact page, all created in Admin as pages (not posts). Home was set to ‘Static’ within the ‘Reading Settings’ and used the front-page.php template file in my child theme directory. The About and Showcase pages all used a template file called page.php as a default.
So I checked the WordPress Template Hierarchy diagram by Rami to ensure my Template Hierarchy was correct, and it seemed to be!
So as a last resort I tried a few other approaches.
1. Firstly I tried the old approach of naming template files, I placed the following comment
/*
Template Name: Contact
*/
in the top of the page-contact.php template file and set the Contact page Template setting to Contact under the page settings in Admin. This worked fine, however I wasn’t happy settling with this approach as it wasn’t used throughout the rest of my child theme.2. Next I tried using a different format for my template file naming, this time i used the page-$id.php format, the id of my Contact page being 54 so the template file would be page-54.php, well that worked too, but why did page-contact.php not work?
3. Next I re-read through WordPress docs on Pages etc, searching for some inkling of insight into what I mite be doing wrong but to no avail! So i decided to try renaming the template file from page-contact.php to page-showcase.php to see if my Showcase page would pick it up instead of the page.php template file, well voila, it did, WTF i thought?
4. Finally out of desperation I just tried removing the Contact page in Admin and recreating it, well guess what, it works now, adopting the page-contact.php template file without a problem. Errr.. OK?
To summarise,
If you ever encounter a situation in WordPress 3.0+ where a page just doesn’t want to use it’s designated page-{slug}.php template file and you have correctly followed the Template Hierarchy rules, then simply try removing and recreating the page in Admin, don’t waste hours like I did.
If someone with the insight into a possible cause could possibly share it with us I would be grateful, thanks in advance.
PS: I still love you WordPress ??
- The topic ‘Template page-slug not working’ is closed to new replies.