How to Theme Nodes of Specific Content Type in Drupal 7


July 4, 2011Drupal10 Comments

There are Basically 2 ways to Theme a page in Drupal
i) By using theme functions in modules
ii) By creating template files in the theme directory.

For the Designer creating the template file would be much more convenient to theme than using the theme function.

Sometimes there may be a need to theme node pages based on content type. In such cases we need to create a template file and create unique styles for that particular node type.

Step 1: If you are using a content type called yourcontenttype, create a template file called node — yourcontenttype.tpl.php and place it in your current theme directory. Say for example suppose if you want to theme blog nodes your filename would be node–blog.tpl.php. Note the two hyphens between node and yourcontenttype is the convention used in Drupal 7

Step 2: Now the Next Step would be to identify the node elements and surround it with your div classes. If you are not sure about the variables copy the contents of node.tpl.php file and paste in your template file.

Step 3: If you dont want to copy node.tpl.php and want to write the template file unique, follow this step.

To Render Differe

nt elements in your template file refer the below syntax

Node Title —

Node Body — – Will Print all the content including images

Node Custom field —

— Here yourfieldname is the machine name of your custom field.

Node Image —

Step 4: If you want to Hide any elements in the content section such as comments use the below statement

Step 5: Suppose if you want to show only if the custom field has a value you can use the below statement

Requirement :- Drupal 7

10 Responses to “How to Theme Nodes of Specific Content Type in Drupal 7”
  1. Thabo says:

    Thanks mate

  2. prasanna says:

    its not working using my content type as node–innergallery.tpl.php

    • Ramesh says:

      When you add theme functions or templates, you need to force the system to update the Theme Registry. Clear the cached data to update theme registry.

      • Max says:

        So I have been about ready to begin swearing like a sailor since I have been trying to do an extremely simple theme customization. All I wanted to do was make my photograph content type appear aligned to the center. No walkthrough I read on mentioned to clear the cache. I know its probably basic, and most people doing this know to do it, but if it wasnt for the comment on this thread, I would still be frustrated as hell right now.

        Thank you!!!!!

        And maybe add that as a step to this walkthrough lol, so that people whether they know to or not are tipped off.

  3. Abdallah says:

    where i can find the first method?
    (i) By using theme functions in modules)

    • Ramesh says:

      You need to understand about theme hooks. Drupal Provides theme hooks in which your data must be passed to format the output. For more clarification refer drupal official site.

  4. woop says:

    EXACTLY what i was looking for — why couldn’t anyone else describe this so succinctly?!

  5. spineless says:

    This is greate when you want a new template for the node content. However I cannot point to a new comment-wrapper–NEWCONTENT.tpl.php to change the comment wrapper. Any idea as to how I can do this?