Last updated August 9, 2013. Created by karschsp on February 19, 2005.
Edited by davidneedham, goron, h_dries, SLIU. Log in to edit this page.

Note: To do this in Drupal 7 please see Drupal 7 Template Suggestions.

The file node.tpl.php is used to theme certain types of nodes. This single file generically covers all Content Types and you can edit it to make changes across the board as detailed on the node.tpl.php handbook page. To theme individual content types in different ways, you need to create a file node-[type].tpl.php in your theme's folder, where [type] is the machine readable name of the content type, for each type you wish to theme. Some examples:

node-story.tpl.php
Themes only Story type nodes.
node-page.tpl.php
Themes only Page type nodes. Note that this is different from page.tpl.php which controls the layout of the entire page including header, sidebars and so on.
node-forum.tpl.php
Themes only Forum type nodes.
node-book.tpl.php
Themes only Book type nodes.

In general, you can replace [type] with any content type system name. One exception is the Flexinode module, which uses numbers to name the types. See Node Templates for Flexinode Content Types. Note that you cannot use node-admin.tpl.php to theme just the admin pages—but you can use page-admin.tpl.php to theme the admin "pages".

Once you have this new file, copy in the contents of node.tpl.php and modify it however you wish. All the variables listed on node.tpl.php can be used.

NOTE: When using phptemplate node-[type].tpl.php suggestions, there must also be an original node.tpl.php template present in your theme directory or the template suggestion is ignored.

This is also the case for subthemes. Just copy the parent themes node.tpl.php into your subtheme folder and refresh the theme cache.

More details can be found at the Core templates and suggestions handbook page.

Drupal 6

For more information on theming nodes by content type in Drupal 6, please refer to the following handbook pages:

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

This technique seems to work for D6 despite only listing "Drupal version: Drupal 4.6.x, Drupal 4.7.x, Drupal 5.x".

In Drupal 7 use two hyphens.

node--article.tpl.php
Themes only Article type nodes.

node--page.tpl.php
Themes only Page type nodes. Note that this is different from page.tpl.php which controls the layout of the entire page including header, sidebars and so on.

node--forum.tpl.php
Themes only Forum type nodes.

Wish I had scrolled down and read this earlier. Two hyphens! Is this a bug that's going to get fixed and then break my template?

Here's the page with full details on D7 tpl files: http://drupal.org/node/1089656

I have node type with machine name _some_thing

What is the name of template?

Oh, thank God finally find the problem! thank you thank you!

Yikai Wang
Web Designer/Developer
http://www.ykwang.co.uk

Dear, please give a template full demo for custom content type.

Bto

See http://api.drupal.org/api/drupal/modules--node--node.tpl.php/6/source for the Drupal 6 version of node.tpl.php

Let's say I've created a content type called "my_content_type". I understand that you could template/theme that entire content-type by creating "page-node-my_content_type.tpl.php". But, what would be the best way to then further customize a single node of that content type (e.g., node 5555)?

I tried the following, but none worked:

page-node-5555.tpl.php
page-node-my_content_theme-5555.tpl.php
node-5555.tpl.php

None of these work. They all continue to use my original content-type template.

You could use
node--5555.tpl.php (two hyphens for Drupal 7)

hi = what if i need to style the node/add or node edit pages? i've tried node-edit-[content-type] and node-add-[content-type] as well as node-[content-type]-add node-[content-type]-edit to no avail.. any pointers welcome

You can try this

<?php
function YOURTHEME-NAME_preprocess_page(&$vars, $hook) {
  if ((
arg(0) == 'node') && (arg(1) == 'add' && arg(2) == 'CONTENT-TYPE')) {
   
$vars['template_files'][] =  'page-node-add-CONTENT-TYPE';
  }
}
?>

I have a question about the specific search pattern drupal 7 uses.

Lets say i have content type "test", "my content type" and "test-test" with machine names of "test", "my_content_type_" and "test_test" respectively. Would the theme file name for each (assuming i wanted to theme by content type) be:

node--test.tpl.php
node--my_content_type_.tpl.php
node--test_test.tpl.php

or are underscores replaced with a hyphon?

If the machine name of the content type contains underscores you translate them to a hyphen.

Example:
node--my_content_type.tpl.php

should be:
node--my-content-type.tpl.php

No need for the trailing hypen or underscore either,

Ie:
node--my_content_type_.tpl.php

Cheers,

J

The Internet has always been, and always will be, a magic box. - Marc Andreessen

Hi

I am pretty new to DP7 7.14. I have setup a content type called Services with the machine name as services. I want the services page(s) to have a different style than the rest of the site.

I have added the following to the template.php file.

<?php
function template_name_preprocess_page(&$vars) {
   
// Add per content type pages
   
if (isset($vars['node']->type)) {
     
// Add template naming suggestion. It should alway use hyphens.
      // If node type is "custom_news", it will pickup "page-custom-news.tpl.php".
       
$vars['theme_hook_suggestions'][]='page_'.$vars['node']->type;
    }
}
?>

I then created a page--service.tpl.php how the services page doesn't work. I am not sure why its not working.

Can anyone suggest why this couldn't be working?

You must write two underscores instead of one.

<?php
function template_name_preprocess_page(&$vars) {
    if (isset(
$vars['node']->type)) {
       
$vars['theme_hook_suggestions'][]='page__'.$vars['node']->type;
    }
}
?>

Now, page--service.tpl.php will work.

I had to use template suggestions in my template.php file. When I did node-[type].tpl.php, it would read that template inside of my entire page.tpl.php template. If you need the system to JUST read the template you're creating for specific content types, try this:

<?php
function THEME_NAME_preprocess_page(&$variables) {
   if (
$variables['node']->type != "") {
     
$variables['template_files'][] = "page-node-" . $variables['node']->type;
   }
}
?>

This worked great for me.

-------------------

*/ no comment */

just wondering about Drupal 8 as trying to theme a custom content type , as there is no documentation on this yet I was first a little shocked that d7 "page.tpl.php" themes files will now be "page.html.twig", which is the scarey brain child of people who brought you the Symfony frame work :(

lets hope the gods Decide the template files can be in ether good old fashion tpl.php as well as for the people who want to mess with .twig ... as I don't see any thing wrong with old fashion php ... except a small (minimal) performance gain.

Any way trying to make a custom template file for the node.html.twig

content type: = test
I have tried :

node-test.html.twig, node--test.html.twig, node-test-content-type.html.twig , node--test-content-type.html.twig , node-test.html.twig , and many more :

sure i can change the node.html.twig to do what i want , but i want to know how to do it like drupal 5, 6, 7 does it.

just so when d-day comes and we all then need to update / upgrade many complex d7 sites to new drupal 8 - we can have a leg up and prepare for the unnecessary.

Is it possible to move the node-[type].tpl.php to a module folder instead that having it in the theme folder?

I have a module that creates a custom content type and I want to provide a default template to drupal_render.

Thanks all.

David

How do I set a node.tpl for each different type of content type?
I am new in drupal and new in this forum. If I have not asked the right questions in the right places, please report to me.

_____________________________
Regards,
Renan Emigdio.