Rahul Prasad
Rahul Prasad

Reputation: 8222

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

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

Upvotes: 15

Views: 32970

Answers (8)

Arul M joseph
Arul M joseph

Reputation: 31

Step 1.create a content type for example: my content type is "fullwidthpage"

Step 2. copy page.tpl.php and rename to page--fullwidthpage.tpl in template folder.

Step 3. Add this code to template.php

function <strong>YOUR_THEME_NAME_HERE</strong>_preprocess_page(&$variables) {
  if (isset($variables['node']->type)) {
    // If the content type's machine name is "my_machine_name" the file
    // name will be "page--my-machine-name.tpl.php".
    $variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type;
  }
}

Don't forgot to change YOUR_THEME_NAME_HERE

Upvotes: 0

harkl
harkl

Reputation: 901

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.

Upvotes: 1

mikeDOTexe
mikeDOTexe

Reputation: 487

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'.

Upvotes: 3

BeWhy
BeWhy

Reputation: 326

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;
  }
}
?>

Upvotes: 19

bitfed
bitfed

Reputation: 345

So depending on what you are trying to accomplish, there might be some CSS tricks that could help you out.

For instance, to override any CSS rules for a specific node type just remember that your pages for that node type will have a body class of "node-type-[TYPE]" (ie: .node-type-article)

You can then override the CSS as follows:

body.node-type-article #selector {}

Upvotes: 0

chadpeppers
chadpeppers

Reputation: 2057

You will need to overwrite the specific node template for the content type if it is anything different than a Basic Page. Please check this out:

http://api.drupal.org/node/19080

Under this page copy/paste the items that are in the node.tpl.php page and overwrite it on how you want. You can access the node specifically by $node

Your template for articles would be:

node--article.tpl.php

Once this is created make sure you clear cache to ensure this works.

Upvotes: 0

Purplemonkey
Purplemonkey

Reputation: 1967

I know this is an old question but if others arrive here, the way I override content types in drupal 7 is like this

node--contenttype.tpl

Upvotes: 0

anotherdave
anotherdave

Reputation: 6744

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

Upvotes: 4

Related Questions