1

I have a content type.

I want to customise the node pages for this content type, I've used node--[content-type].tpl.php but I need to use a page template to customise the header and footer and page--[content-type].tpl.php doesn't work.

I've tried page--node.tpl.php, but this changes the home page as well, then i created page--node--[content-type].tpl.php which is not working.

Please help me.

Thanks

  • Should i use page--node.tpl.php but this will go for all content type. – asalug Apr 1 '14 at 6:31
2

This is explained on this page at Drupal.org.

Below is a more detailed description about how you do this in Drupal 7:

  1. Open the template.php file in your theme for editing.
  2. Look for a function called THEME_preprocess_page (replace THEME with the theme's name).
  3. If this function already exists, you will need to add your code to it inside an if-statement that goes at the end of this function, just before the closing bracket.
  4. Otherwise, if it doesn't exist, you'll need to create a new function:

This is how the function should look like:

function THEME_preprocess_page(&$vars) {
  // If the function already exists, just leave the code here intact, and add
  // the three lines below to the end.
  if (isset($vars['node']->type)) {
    $vars['theme_hook_suggestions'][] = 'page__' . $vars['node']->type;
  }
}

NOTE: do not string replace any underscores inside the content type's machine name if you're using Drupal 7 (I think you had to do that in Drupal 6).

Now, copy the theme's page.tpl.php to a file named page--contenttype.tpl.php and place it inside the themes templates/folder. For contenttype, use the machine name of the content type. You can inspect the machine name if you navigate to Administration » Structure » Content types.

Now, edit page--contenttype.tpl.php so that the header and footer is changed the way you want. Do not change the parts you don't want to appear changed.

All nodes with content of that that type will use the new template file.

Provided you're using Drupal 7, observe the following conventions when naming the template override file.

  • Use two hyphens to separate the elements in the filename.
  • If you're overriding a content type, and your content type is two or more words, replace the underscore character (_) in the machine name with a single hyphen (-) in the file name.

For instance, the name of a template override for content type with a machine name my_machine_name should be named (in D7): page--my-machine-name.tpl.php.

  • This working but the changes also reflecting in content-type page. – asalug Apr 1 '14 at 9:39
  • yes, i did exactly the same which you have explained me.let me elaborate all the steps i have done. 1. i declared the function in the page.vars.php because that function is already declared in the file and i have just added this code, and then created a file named as page--[content-type].tpl.php(content type name is a one word and same as machine name),but the changes i have made in this file reflecting on drupal/content-type/nodes pages(that's what i need)as well the page created by views named as drupal/content-type which i don't want this. – asalug Apr 1 '14 at 10:24
  • the same i was thinking, because it worked fine in my dummy site, wel thanks for everything let me try changing the views path and let see, Thanks buddy.. – asalug Apr 1 '14 at 10:38
5

You can have page templates based on a content type, you just have to add some code so Drupal will recognise the template file: This link shows how to do this.

It depends on your Drupal version. You would think this would be default but for some reason, probably performance related, it is not.

For D7 add this to the template.php file in your theme directory.

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

End now you can customize your footer header etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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