3

I've started looking at content type specific node.tpl.php files. However, say I have two content types, and wish both to be displayed in a similar manner, but not apply this to every content type (at the level of node.tpl.php).

It seems a bit of an overkill to have two identical files for node--contenttype_1.tpl.php and node--contenttype_2.tpl.php which are the same? Is there a way to have a multi-content-type template for this?

In case you're wondering the reason for having two different content types in the first place, one is a simpler version of the other, has different URL generation rules (via pathauto), and fewer fields associated with it.

2 Answers 2

8

Implement a preprocess function that adds a template suggestion.

Assuming your theme is called EXAMPLETHEME, your content types are content_type_1 and content_type_2 and the template file is called node--example.tpl_php, add the following function to the file template.php of your theme:

function EXAMPLETHEME_preprocess_node(&$variables) {
  $node = $variables['node'];
  if ($node->type == 'content_type_1' || $node->type == 'content_type_2') {
    $variables['theme_hook_suggestion'] = 'node__example';
  }
}
0
0

An "evil" suggestion would be to create a symlink node--contenttype_2.tpl.php which points to node--contenttype_1.tpl.php. I have actually used stuff like this on production sites.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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