OK so i am new to drupal. I never used Drupal 6 and know more than the basics for drupal 7. But one thing that i need to know while i am creating my drupal theme is how to specify a page.tpl file for a content type. Ex. I have a content type named Blog. i want to create a file to change the look of all the blog pages that it creates. While i was online i learned that in Drupal 6 you just change the file name to page-(content type name).tpl.php it didn't work for drupal 7. can anyone please help me.

Comments

gapa’s picture

Have you tried with page--(content type name).tpl.php?
Also check this:
http://drupal.org/theme-guide/6-7
http://drupal.org/node/1089656

koahmad98’s picture

Yes i did try it and it didnt work at all. I spend about an hour trying many different things and none of them worked. i tried: page--type--blog(content type name).tpl.php , page--node--blog.tpl.php, page-blog.tpl.php, and many more.

gapa’s picture

koahmad98’s picture

Yes i did and it didnt work.

gapa’s picture

hmm. Do you use views to display blog posts? if so you could maybe try to use views theming. if not how do you display them?
If you display only one article node on the page than this http://drupal.org/node/1035656#comment-3997484 works for me. Tested.

And of course don't forget to clear cache every time you try something new.

dvessel’s picture

The only thing "page" knows about are the path arguments. Nothing is in there to pick-up on node types. You need to help it out.

Here's some code which should go into your template.php file and THEME_NAME must be replaced with the machine readable name of your theme.


/**
 * Variables preprocess function for the "page" theming hook.
 */
function THEME_NAME_preprocess_page(&$vars) {

  // Do we have a node?
  if (isset($vars['node'])) {

    // Ref suggestions cuz it's stupid long.
    $suggests = &$vars['theme_hook_suggestions'];

    // Get path arguments.
    $args = arg();
    // Remove first argument of "node".
    unset($args[0]);

    // Set type.
    $type = "page__type_{$vars['node']->type}";

    // Bring it all together.
    $suggests = array_merge(
      $suggests,
      array($type),
      theme_get_suggestions($args, $type)
    );

    // if the url is: 'http://domain.com/node/123/edit'
    // and node type is 'blog'..
    // 
    // This will be the suggestions:
    //
    // - page__node
    // - page__node__%
    // - page__node__123
    // - page__node__edit
    // - page__type_blog
    // - page__type_blog__%
    // - page__type_blog__123
    // - page__type_blog__edit
    // 
    // Which connects to these templates:
    //
    // - page--node.tpl.php
    // - page--node--%.tpl.php
    // - page--node--123.tpl.php
    // - page--node--edit.tpl.php
    // - page--type-blog.tpl.php          << this is what you want.
    // - page--type-blog--%.tpl.php
    // - page--type-blog--123.tpl.php
    // - page--type-blog--edit.tpl.php
    // 
    // Latter items take precedence.
  }
}

And this will work for all node types and look! You can even have a custom template for editing your blog. It's easy to get carried away with this stuff though so be careful. If you have tons of templates, you're doing something wrong.

johnlaine’s picture

Found your post and it was exactly what I needed. Thanks for the detailed commenting in the code.

Anonymous’s picture

Thank you for posting such helpful info

ocamp’s picture

is this just for 7 or will it work on drupal 6 aswell?

ghan’s picture

Thanks for the detailed tip -- saved me a lot of headache!

asonderer’s picture

Hi, same effects for Drupal 6 with this in you templatename_preprocess_page:
if (isset($vars['node'])) $vars['template_files'][] = "page--type-{$vars['node']->type}";

gerardw85’s picture

Your explanation is fantastic - Thank You!

SKAUGHT’s picture

+1

sh radhoush’s picture

thanks for your helpful post!
I have a site with 120 categories of content. I've used taxonomy to make these categories. now I need to have some different styles base on the taxonomy which is used in nodes. OK? how can I do it.

I mean here different content types has change to different taxonomies! now how can I implement different styles for theme.

as my website is very big and heavy and has 700k visitors daily, I avoid using modules like panels or context! this modules will kill my site's performance and speed! so I need simple approach without using modules.

Jaypan’s picture

You can add template suggestions for the relevant taxonomy terms, and have separate templates accordingly:

https://www.drupal.org/node/1089656

venkatesh.rajavetrivel’s picture

Hi,

Guess, we can create a tpl with "node--{content-type-machine-name}.tpl.php" which will render template specific to the Content Type. It just works fine for me.

rick_p’s picture

@ dvessel, All these years later you're still helping people with this post (instruction). I was getting no results after creating an override tpl file for a custom content type in my Bootstrap sub-theme. After adding this code to my pre-existing but empty template.php, my tpl file fired up instantly.

Many thanks!! Rick

Mitnick’s picture

This is exactly what I was looking for my custom theme. Thank you so much for sharing this solution.

originalscott’s picture

I looked for this for a few days when I first started looking at drupal and still cant believe that it is so hard to have a different template based on page type, I am building a base site for 8 other websites in the accommodation industry so it needs to be flexible with page layout with different accommodation types.

What I have found is that all the help and instructions are for drupal 6 , it is quite hard to find the different settings etc for Drupal 7.

I thought I would post this as a help to any one trying to have a different .tpl page for page type.

Anyhow what I have done for this problem.

For Drupal 7

Go to http://www.yoursite.com/user and log in.

Then go to Structure > Content types + Add Content Type
Add Name we will use the name example so type in example (Take note of the machine name which in this case will be example)

Enter a description of the new page so people know what they are adding to the site.
As we are just adding a basic one now so please now go to the bottom and click save content type.

Ok now we have the content type example in the list. When you go to Content > + Add content there is a content type “example” there.

Now we need to go to our theme and add the code below in your template.php file. Take note of two things

1. Search the template.php file first to see if there is already and instance of _preprocess_page . If you have used a template to start from then this may already be in the template.php if it is then please add the code indide the function to the existing function.

2. Secondly change the YOURTHEMENAME to your theme name and leave the _preprocess_page there. (I know this sounds like everyone should know this and I did but my Junior programmer struggled with this part).

// Copy from here Template for Content type

function YOURTHEMENAME_preprocess_page(&$vars) {
    // - page--example.tpl.php  

 if (isset($vars['node'])) {
$vars['theme_hook_suggestion'] = 'page__'.$vars['node']->type; //
}
}
// Finish copy Template for Content type

Now in the same folder that you have your page.tpl.php file create another template file (remember we are using example)

The file name would be page--example.tpl.php

IMPORTANT!
Now upload the files and then CLEAR THE CACHE Go to configuration > Performance and click clear all caches.

You will now have a different .tpl.php page for each page type you would like.

From a users point of view this (In my opinion) is much easier for them to grasp when they are adding pages to the site that they are adding a page for a specific type of content.

This is my first post hope there are many more, thanks everyone for all the help. Scott

F.G’s picture

thanks, Scott.

jorgsuau’s picture

This works perfectly. :)

W.M.’s picture

Thank you very much.

ramesh.p’s picture

spineless’s picture

This is great to help point to a new page template based on a content type. I am attempting the same thing but for a comment form.

I thought it would be simple to just create a new comment-wrapper form like this.

comment-wrapper--NEWNODETYPE.tpl.php

But the new comment form did not appear. Not sure what I need to do. Any idea?

chadmandoo’s picture

It seems that this is a little overkill when we simply need to make a node template for the comment type. For example for the node type blog, assuming the machine name is blog, the node template would simple be node--article.tpl.php.

Here you can display and access anything in the node by dumping the $node object (using dpm($node) or whatever way you want to dump the object).

Here is a link to the node template file: http://api.drupal.org/api/drupal/modules%21node%21node.tpl.php/7

ek.senthil’s picture

Add this code in template.php in the Bootstrap Subtheme

//page--content-type-name.tpl.php
function MY_BOOTSTRAP_SUBTHEME_preprocess_page(&$vars, $hook) {
if (isset($vars['node']->type)) {
$vars['theme_hook_suggestions'][] = 'page__' . $vars['node']->type;
}
}

geraldito’s picture

thanks @eksenthil, this works fine for D7

sudharson’s picture

Add the below code in the template file

function my_theme_reprocess_page(&$variables, $hook) {
if (isset($variables['node']->type) && !empty($variables['node']->type)) {
$variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
}
}

shashikanthd’s picture

I am trying to know abt priorities of tpl files in themes, from this post i got what exactly hook suggestions at drupal..

ltaliano’s picture

This code works for me, when viewing or editing a content type, but when add a new one it doesn't.  I can't figure out what to name the template.  The URL is http://domain.com/node/add/content-type-name/436