Theming nodes by content type

Last modified: April 14, 2009 - 22:52

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 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:

Thanks for "... there must

motto - June 11, 2009 - 15:24

Thanks for "... there must also be an original node.tpl.php template present in your theme directory or the template suggestion is ignored."!!
I started getting frustrated with PHPTemplate for not picking up my custom node-project.tpl.php... No I understand why.

 
 

Drupal is a registered trademark of Dries Buytaert.