Creating Custom Content Type Page Templates with Drupal & PHP

Jun 2 2010

Creating Content Type Page Templates with Drupal & PHPWe were recently asked to create custom landing pages for a Google ad campaign for a Drupal site we developed. The issue we faced was that the layout for the landing page was not really close to anything in the existing theme.

Drupal by default can do custom node type templates based off any given custom content type (node-blog.tpl.php for example) but all that does is theme and customize the content area. In our case we needed to change the entire page layout. Well it turns out there is a Drupal PHP preprocess function that can help accomplish custom page templates.

For this to work you need to alter template.php in your Drupal site theme folder. If you don’t have this file yet, you can go ahead and create a blank one that you will add code to.

In your template.php file add this code:

  1. <?php
  2. /**
  3. * Add / modify variables before the page renders.
  4. */
  5. function phptemplate_preprocess_page(&$vars) {
  6.  
  7. // custom content type page template
  8.   // Renders a new page template to the list of templates used if it exists
  9.   if (isset($vars['node'])) {
  10.     // This code looks for any page-custom_content_type.tpl.php page
  11.     $vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);  
  12.   }
  13. }

And here is how this works: Lets say you create a custom content type called "Landing" with a machine name of "landing'. You could duplicate your page.tpl.php, rename it to page-landing.tpl.php and then customize the entire layout of that page with CSS & HTML as per your specifications. Now when you have any pieces of content that are designated as Type "Landing" the new custom template you just made will be used automatically to render that content. Remember to clear your Drupal cache for this to work.


works like a charm!

awesome, works like a charm! thanks a lot!

about page.tpl.php

please can u clearly explain me what exactly page.tpl.php does.
thanks in advance

page.tpl.php is the main page

page.tpl.php is the main page of a Drupal theme using PHPTemplate.

but How do you delete custom pages...

hi,

you are probably aware that you can create your custom page as page-node-myPage.tpl.php and place it in the theme directory and Drupal will pick it up.... this is useful when your page is used once and does not warrant creating a "content type" for it.
but i though i will try your method above, and it works fine....well except untill i wanted to delete a test page i create it. when you go to list content page, it shows me the page that i created usign the new custom type with the edit link next to it. so i click on that hoping i get an option to delete that page as usual, but i don't see the delete button anymore.... any ideas???

regards

Yes, as I mention you can do

Yes, as I mention you can do custom node pages out of the box with Drupal but I needed page-[custom].tpl.php in order to do completely different layouts such as a landing page. As far as deleting a node associated with a custom page template, I have not experienced that. Did you try clearing your cache? I will look in to it a bit further.

Clearing cache did not bring

Clearing cache did not bring up any delete button... Just as a reminder , I can delete the custom TYPE but I cannot delete the content created using that type.

Ok, I'm not sure why that's

Ok, I'm not sure why that's happening. I'll look into it.

delete page.

check that content and select delete from update option now click update. i suppose you were talking about it.

Well, I am so excited that I

Well, I am so excited that I have found your post because I've been searching for some good info regarding it for almost 3 hours. You helped me a great deal without a doubt

Excelente articulo.. me

Excelente articulo.. me sirvio de mucho!! mil gracias

Not working for me...

Thanks for the post, I have been looking for this exact thing but have had no luck with anything I have tried. So I used CCK and create a custom content type called contact with a machine name of contact. I created a page with content type contact, and a file in my themes folder called page--contact.tpl.php. I copied the code into my template.php file at the end of the existing code, removing the closing php tage from the existing code and the new php tag from the new code (I also tried it without doing this).

Then I proceeded to remove things from the code in page--contact.tpl.php and reloading my page to see the difference and nothing happened. I ran update.php, cleared the cache, and ran it again. Any ideas? If it helps I am running it locally off of Xampp.

Thanks

Did you try

Did you try "page-contact.tpl.php" with only 1 dash? What version of drupal are you using. (It sounds like 6 because you mentioned CCK -- so that should work. I am not sure if using the name "contact" would conflict with something in Drupal.

custom template page not displaying

i have pasted the 14 lines of code exactly as they are typed at the very top of my template.php file in the root directory of my [Marinelli] theme.

i have created a content type called "Project" with a machine name "project" and i have duplicated my "page.tpl.php" page and renamed it "page-project.tpl.php" keeping it in the same directory as page.tpl.php.

i've cleared cache, but it is still not picking up my new template page.

what am i doing wrong?
thanks for any advice!

cheers, Jean

Please note, you need to

Please note, you need to remove the closing ?> php tag as a commenter mentioned above. I have adjusted the code. What version of Drupal are you using? It sounds like you did everything right.

custom template page (still) not displaying

thanks for your reply.
however, removing that closing "?>" tag gives me the following error on my site:
Parse error: syntax error, unexpected '<' in /home/content/j/m/a/jmatuska/html/dr/sites/all/themes/marinelli/template.php on line 17

i figured that would happen since the very next line in my template.php file starts with an opening <?php tag. so, i'm thinking i still need that closing php tag there on line #14.

here is the original template.php code, now starting on line #17:

  1. <?php // $Id$
  2.  
  3. // we define a global tag to use in diferent templates
  4. define('OUTTAG', ( theme_get_setting('outside_tags') ? 'p' : 'h2' ) );
  5.  
  6. include_once('theme/theme.inc');
  7. include_once('logics/layout.inc');
  8. include_once('logics/banners.inc');
  9.  
  10. /**
  11.  * Additional page variables
  12.  */
  13. function marinelli_preprocess_page(&$vars) {
  14.        
  15.   // Useful for devel default banners, remove before commit
  16.   //   variable_del('theme_marinelli_first_install');
  17.   // Chcek if is first setup of marinelli and install banners.
  18.   if (variable_get('theme_marinelli_first_install', TRUE)) {
  19.     include_once('theme-settings.php');
  20.     _marinelli_install();
  21.   }

?>

thanks again.
:) Jean

Are you using the Marinelli

Are you using the Marinelli theme from drupal.org -- http://drupal.org/project/marinelli If so I can download the theme and test it out. What version of Drupal are you using?

make a custom template page for different page

If your using drupal 7 plz copy the below code in your template.tpl.php file
in drupal 7 copy the page.tpl.php file rename page.tpl.php to page--custompage_name.tpl.php

  1. function bartik_preprocess_page(&$vars, $hook) {
  2.   // change bartik to your theme name ex.if theme is Danland then danland_preprocess_page(&$vars, $hook)
  3.   if (isset($vars['node'])) {
  4.   // If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
  5.    $vars['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $vars['node']->type);
  6.   }
  7. }

  1. you can try this for drupal 6
  2. In drupal 6 copy the page.tpl.php file rename page.tpl.php to page-custompage_name.tpl.php
  3. and past the below code in template.tpl.php
  4.  
  5. function phptemplate_preprocess_page(&$variables) {
  6.  
  7. // custom content type page template
  8.   // Renders a new page template to the list of templates used if it exists
  9.   if (isset($variables['node'])) {
  10.     // This code looks for any page-custom_content_type.tpl.php page
  11.     $variables['template_files'][] = 'page-'. str_replace('_', '-', $variables['node']->type);  
  12.   }
  13. }

hope it will help you

custom template page (still) not displaying

yes, am using that Marinelli theme. and my Drupal install is v7.4

Have a look at this, it might

Have a look at this, it might help: http://drupal.org/node/982122

When I wrote this post,

When I wrote this post, Drupal 7 was not out yet so this applies to Drupal 6. I have no idea if this works for Drupal 7. Sorry, I don't know when I will have time to test it for D7.

Thankyou

Hi, rockmedia

You are really rocking! Very nice i did it.

Thanks :)

Updated for Drupal 7

Hi. I have managed to get your idea to work in drupal 7. Everyone will like this in that
it works out more easily in D7. ☺☻

I put the following in template.php ...

  1. function bartik_preprocess_page(&$vars) {
  2.   if (isset($vars['node'])) {
  3.   $vars['theme_hook_suggestion'] = 'page__'.$vars['node']->type; // string becomes 'page__dog_page'
  4.   }
  5. }

(real) example:
contentType: dog_page **must use the Machine name
custom .tlp file name: page--dog-page.tpl.php

The tough part is getting the naming all right. 'page__dog_page' must be converted by you by
replacing all underscores___ with hyphens--- to set the name for your .tpl file. So D7 does not
need the str_replace that D6 did. Strings use __ and file names use --.

People can check out this

People can check out this blog post: http://www.digett.com/blog/01/11/2012/overriding-page-templates-content-... -- which essentially uses Randy's code above.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
3
G
M
A
4
3
1
u
Enter the code without spaces.
To prevent automated spam submissions leave this field empty.