Drupal 7 How to override page.tpl for specific content type?

32,747

Solution 1

You have to make sure that your template can handle this ... I got a code snippet that worked for me here:

http://drupal.org/node/1089656#comment-4426790

<?php
function themeName_preprocess_page(&$vars, $hook) {
  if (isset($vars['node'])) {
    // If the node type is "blog_madness" the template suggestion will be "page--blog-madness.tpl.php".
    $vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type;
  }
}
?>

Solution 2

Have you remembered to clear the cache (under Administer > Site configuration > Performance) so that Drupal is 'aware' of your new file & uses this rather than the default.

See here on Clearing cached data

Also, you may need to add a preprocess hook (I haven't used D7 myself, just 5/6 — think this has changed slightly.) This post on Drupal Stack Exchange seems to give more details

Solution 3

I just ran into the same problem.

node--recipe.tpl.php

not

page--recipe.tpl.php

this is, of course, with a content type called 'recipe'.

Solution 4

ALWAYS CLEAR THE CACHE...

Go to /admin/config/development/performance and click the clear cache button. It should be the first thing you do if you've made alterations to the structure of the backend and they're not showing as expected.

Otherwise you almost had it right

  1. page--content-type-name.tpl.php

will allow you to theme the content type template.

Share:
32,747
Rahul Prasad
Author by

Rahul Prasad

Entrepreneur and opensource enthusiast Currently learning GoLang Lisp Blog: http://blog.rahulprasad.com Linkedin: http://in.linkedin.com/in/prasadrahul/ Github: https://github.com/rahulpache Twitter: https://twitter.com/rahul_pache

Updated on May 07, 2020

Comments

  • Rahul Prasad
    Rahul Prasad about 4 years

    I wanted to override page.tpl.php for specific content type. I have tried these thing, nothing works for me.

    1. page--article.tpl.php
    2. page--node--article.tpl.php
    3. page--node--type--article.tpl.php
    4. page--node-type--article.tpl.php
    5. page--type--article.tpl.php

    But when I targetted specific node by number i.e. page--node--8.tpl.php it worked fine. I think page--article.tpl.php should have worked, but I dont know why its not working.

    Please tell me if I am naming it wrong. and how can I debug such things. I have heard I can use Devel module, but know nothing about it. A slight hint in right direction will be appreciated.

    Thanks in advance

  • AKS
    AKS almost 12 years
    Drupal code theme suggestions does not have a node type template suggestions. So you must add a theme suggestion function to your template.php (See the above link) :)
  • chadpeppers
    chadpeppers about 11 years
    You need to overwrite the node template file specific if its not a basic page. No need to do the suggestion in the template.php if you are overriding the entire node