Custom page template for specific node - template inheritance I'm trying to customize the page template for a specific node of a specific content type. This is what I have: Content type: Custom Page Node ID: 2

First, I need to customize all 'Custom Page' content nodes so I created a custom page template for them: page--custom-page.tpl.php

Then, I also need to customize a specific 'Custom Page' node so I created a custom page template for it too: page--node--2.tpl.php

But when I visit the node 2 page it is using the Custom Page (page--custom-page.tpl.php) template and not the page template I created specifically for that node.

How can I make the custom page template for that node work?
Templates are chosen based on the other that they are in the $variables['theme_hook_suggestions'] array.

So if you have a list:

page.tpl.php
page--node.tpl.php
page--node--1.tpl.php
page--node--edit.tpl.php


Then it will look from the bottom up and use the first template it finds.

From Drupal 7 Template (Theme Hook) Suggestions:


  When the page is actually rendered, the last suggestion is checked. If it exists, that suggestion is used. Otherwise the next suggestion up is checked, and so on. Of course, if none of the overriding suggestions exist, page.tpl.php is the final suggestion. This also explains why page--front.tpl.php, if it exists, overrides any other suggestion for the front page: it is always the last suggestion for the page designated as the front page.


Because you have custom code (I'm assuming your code here) that adds a tempalte suggestion to the end of that like this:

$variables['theme_hook_suggestions'][] = 'page__custom_page';


Then that suggestion is added to the end of the list, so will override all previous ones if it is present.

If you want to have different template precedence then you have to make the hook suggestions array use that order.

There are a number of ways you can do this and it depends what you want to achieve as to how you want to do it.

If you want your template to be last in the template precedence (first in the array) then you can do this:

array_unshift($variables['theme_hook_suggestions'], 'page__custom_page');


Other than that you can also use other PHP array manipulation functions to insert your suggestion in the middle of the array, or you could add your suggestion to the end, then add other suggestions after it again (it doesn't matter if a suggestion is in the array multiple times, the first time it is checked it will be used).