Free Videos about Mastering Views

How To: Unique Pages by Content Type

Play Video
Movie link (right-click to download)
Problem with this video? Contact me

Drupal provides a lot of default template files when it comes to page, node and block output.

There are, however, a few instances when you may want to theme a page based on the content type - such as having a dedicated page for when videos are viewed (I'm doing just such a thing on this site).

What you need to do then is tell Drupal about the dedicated and specific template file you wish to use for that content type. The way to do this is with preprocess functions.

This video shows you the few lines of code you need. Of course, if you don't want to watch the video then all you need is this.

<?php
/**
* Adding or modifying variables before page render.
*/
function phptemplate_preprocess_page(&$vars) {

 
// Page change based on node->type
  // Add a new page-TYPE template to the list of templates used
 
if (isset($vars['node'])) {
   
// Add template naming suggestion. It should alway use hyphens.
   
$vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);   
  }
}
?>

Very useful, but 2 things you didn't mention in text (I didn't have time to watch video)

  1. This goes in template.php?
  2. It's for Drupal 6.0?

Are the answers to the above questions.

Thanks for the sharing. The video is very good, I like to watch it and I have download this one. Thanks! classifieds |ads|orange county restaurants

But it doesn't seem to be working for me (I'm making a Zen sub-theme). I've cleared the caches and tried multiple content types / tpl files with no luck.

Also, what does this snippet do to page.front.tpl.php? Does it break it? Do you have to use the node id for your tpl file instead?

Any feedback you might have would be awesome.

Well, it depends on your current "function phptemplate_preprocess_page" in your theme.
For me, I'm working with Basic theme and I have an allready phptemplate_preprocess_page function :

function phptemplate_preprocess_page(&$vars, $hook) {
  ......

I have put this code into :
  // Add page template suggestions based on node type, if we aren't editing the node.
  if ($vars['node'] && arg(2) != 'edit') {
    $vars['template_files'][] = 'page-nodetype-'. $vars['node']->type;
  }

ie: now I can use a page-nodetype-actualite.tpl.php for specific "actualite" content type
have a look here :
http://drupal.org/node/249726

Hey Matt thank you so much for this video... In you site I get closer and closer to what I want to do in drupal. There is still one, actually a few things that I cant understand tough, I'm sorry for being such a newbie :P.

I succeeded in creating a page specifically to my content type.
It shows the info I entered on the content. In the new page I created.

What I cant do yet is to put components of the $content area in other areas.. like for instance on the Book form, I created a image cache file field with CCK to upload images... and when I load the submitted book page it shows the picture on the $content area.. how can I change that to the $left sidebar for instance?
I installed the Dev tools.. but I still don't know how to use the variables.

Please can you help me on that? I read so many pages and so many books but I don't know why I still have not grasped the concept.
Should I do this with Views? Or Panels Or just create Blocks?... I'm lost :S

BTW I forgot to add my name on the post above. Nice to meet you all I'm Gill. :)

what is the correct way of calling page by node type... page-node-my_content_type or page-my_content_type?

thanks,

vsotto

This helped me out greatly. I was wondering though how would I also include the "create new content" page of that content type. Im not sure if that sounds right.

For example now my new custom templates renders when I go to:

www.mysite.com/my-node-type

I would also like the same template to render when I go to:

www.mysite.com/add/my-node-type

I tried adding this but didnt seem to work:

<?php
 
if (isset($vars['node/add'] )) {
   
$vars['template_files'][] = 'page-'. $vars['node']->type;  
  }
?>

Thanks!

JC

Oh and one other thing. Im doing this template thing for the same edit you did by just removing the sidebars.

So I was wondering is it possible to just create a template page maybe called page-wide.tpl.php and then be able to tell what pages and content types to use page-wide.tpl.php so that way I wouldnt need to keep creating more template pages that are the same thing.

I hope what I said makes sense.

Thanks!

JC

Wow, it took me a while but I found how to do this from http://drupal.org/node/408530 in case this helps anyone else.

I just had to create a page template called page-node-add-content_type.tpl.php

JC

Hi Matt

I want to start by thanking you for wonderful screen casts on your website. You really have a talent for complicated things in an easy and understandable way. you really saved my a lot of time:)

I watched your screen cast on theming and i was hoping if you could an more advantaged theming tutorial. Like for instance the combination between php and css and how you can alter the layout (move text, make letter bold etc.)

I watched your screen cast here about template files and pre-processes which i found very useful. I managed to create my own template file for my specific content type by using the code but now i need to take it step forward

Greetings from freezing Denmark

Thanks for the great info and well-presented format. I will follow this site regularly as I learn the ways of Drupal.

In the video (Unique pages by content type) you describes how to add a page-video.tpg.php and add the php

I have done this, I have created a view to display new video content type on path videos so when I click on the tytle it takes me to the full node where the information is displayed.

I'm using Zero Point Theme and the function below is in my template.php file, I have added the code snippet.
is this correct?
Dose this detect the node video when you embed one on a page? (body)
I see that you create a new video content type but how dose it link to the video?
are you creating a content type video display? and use the url in the view?
is it on a separate node or on the same node?
Maybe the same question to find the same solution!
/////////////////////////
function phptemplate_preprocess_page(&$vars) {
// Remove the duplicate meta content-type tag, a bug in Drupal 6
$vars['head'] = preg_replace('/]*>/', '', $vars['head']);
// Remove sidebars if disabled
if (!$vars['show_blocks']) {
$vars['left'] = '';
$vars['right'] = '';
 }

// Build array of helpful body classes
$body_classes = array();
$body_classes[] = 'layout-'. (($vars['left']) ? 'left-main' : 'main') . (($vars['right']) ? '-right' : ''); // Page sidebars are active (Jello Mold)
$body_classes[] = ($vars['is_admin']) ? 'admin' : 'not-admin'; // Page user is admin
$body_classes[] = ($vars['logged_in']) ? 'logged-in' : 'not-logged-in'; // Page user is logged in
$body_classes[] = ($vars['is_front']) ? 'front' : 'not-front'; // Page is front page
if (isset($vars['node'])) {
$body_classes[] = ($vars['node']) ? 'full-node' : ''; // Page is one full node
$body_classes[] = (($vars['node']->type == 'forum') || (arg(0) == 'forum')) ? 'forum' : ''; // Page is Forum page
$body_classes[] = ($vars['node']->type) ? 'node-type-'. $vars['node']->type : ''; // Page has node-type-x, e.g., node-type-page
$body_classes[] = ($vars['node']->nid) ? 'nid-'. $vars['node']->nid : ''; // Page has id-x, e.g., id-32
}
else {
$body_classes[] = (arg(0) == 'forum') ? 'forum' : ''; // Page is Forum page
}
////////////////////////
// Page change based on node->type
// Add a new page-TYPE template to the list of templates used
if (isset($vars['node'])) {
// Add template naming suggestion. It should alway use hyphens.
$vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);
}
///////////////////////
// Add any taxonomy terms for node pages
if (isset($vars['node']->taxonomy)) {
foreach ($vars['node']->taxonomy as $taxonomy_id => $term_info) {
$body_classes[] = 'tag-'. $taxonomy_id; // Page has terms (tag-x)
// $taxonomy_name = id_safe($term_info->name);
// if ($taxonomy_name) {
// $body_classes[] = 'tag-'. $taxonomy_name; // Page has terms (tag-name)
// }
}
 }