When you first install a Drupal 7 site with a base theme, you’ll get a couple of standard templates such as
page.tpl.php. But often you are going to need to create a node template for a specific content type.
Using templates for content types gives you a lot of control. You can do things like:
- hide fields
- reorder fields
- adding wrapper classes for styling
Before we start, let’s get a brief intro into the difference between a node and a page template.
- Page: the template that controls the whole page. This will include a
$contentvariable, which after rendering will be the node
- Node: the template that controls just the content part of the page.
In most cases when creating a template for a specific content type, you’ll be creating a node template. Within the template, you might be rendering specific fields and doing other things on the node level. It is also possible to create a specific page template for a content type, which we will cover in another tutorial.
The easiest way to create a starting point for your content type node template is to copy the existing node template in your theme. And then you’ll need to rename it according to the following naming convention:
The machine name for the “Article” content type is
article. So the node template for the “article” content type will be:
After you have saved the template, clear the cache (go to Administer » Site configuration » Performance » Clear cached data, or use Drush).
Drupal 7 template suggestions
When creating a template like this, you aren’t actually guaranteed that Drupal will use the template. There could be more than one node template in a Drupal site, so Drupal has to decide which one to use. Drupal will order the template files in the following order:
If you had a template for a specific node that would be used instead of the template for the content type. For example,
node--18.tpl.php would be used for the node with a NID of 18.
Creating a template for a specific content type is just a matter of getting the naming convention right. You don’t need to add any additional code to tell Drupal about the template, it will scan the theme files using the pre determining naming conventions.
Theme suggestions on Drupal.org