Hey,

Trying to create a page override but it's not taking. The strange thing is it's working for a view I created. If you go to:

www.kaymerica.com/portfolio - this page is a view page which is using my page--portfolio.tpl.php override. My problem is, if I click on a piece of content there, it refuses to use the same page display.

eg. www.kaymerica.com/portfolio/a2zane

That piece of content is a "portfolio" content type and should use the override that views seems to be finding.

Anyone have any idea what I'm doing wrong?

I've done this a million times but this is my first D7 installation so I'm not sure what I could be doing wrong.

Cheers,
Michael

PS - I clear my cache about 500 times a day. So I know that's not the issue. Cheers....

Comments

robert broen’s picture

This is what works for me:

In my theme folder I have a template.php file with the following code:

<?php
function sev_preprocess_page(&$variables) {
    if (isset($variables['node']->type)) {
        $nodetype = $variables['node']->type;
        $variables['theme_hook_suggestions'][] = 'page__' . $nodetype;
    }
}

Create a file 'page--yourcontenttype.tpl.php' in your templates folder (replace 'yourcontenttype' with something else).
Clear the cache and reload your page.

Good luck!

kaykills’s picture

Thanks for the reply. I've added that code with no success. I think maybe something has happened with the content type. It's strange that my views page uses my custom page template, but when I actually go to a piece of content using that content type, it doesn't work. Not sure what else to do.

kaykills’s picture

Hey... actually this did work. I am an idiot.

vlison’s picture

After I read the above I truly got frustrated. After having searched and tried for a few hours, having found the above solution after about 10 minutes (on several websites) it still didn't work here...

I had tried 'page--' and 'page__', used the same characters for the template filename.

I guess you idiot did something alike ;)

The solution is : the above answer is correct! BUT (watch these characters : ' - ' and ' _ ')

* in the template.php file you write : 'page__' . $variables['node']->type;
* as filename you use "page--contenttypemachinename.tpl.php"

Enzman’s picture

Can't follow the instructions above. My template.php file has a lot of stuff in it. Do I add the code (if yes, where - at the beginning, end or somewhere in the middle?), do I replace the my template.php file with one that just has that code in it?

PS: am now going to try a whole lot of stuff... or search for instructions that compensate for my 'idiotness'.

Enzman’s picture

Took two hours to find the simple change required.

I found that this helped: https://drupal.org/node/1089656

Where it says:

Add a page.tpl.php depending on content type.
In Drupal 7, you do this by having this code in the function THEME_preprocess_page (where THEME is the name of the theme) in the theme's template.php.

function THEME_preprocess_page(&$vars, $hook) {
  if (isset($vars['node']->type)) {
    // If the content type's machine name is "my_machine_name" the file
    // name will be "page--my-machine-name.tpl.php".
    $vars['theme_hook_suggestions'][] = 'page__' . $vars['node']->type;
  }
}

Add the if-statement at the end if this function already exists in the theme's template.php, otherwise, add the functions.

Using a Zen Subtheme, all I had to do was to go to the template.php file, find the second instance of "preprocess_page" and delete this line which was the line above it, in its entirety:
/* -- Delete this line if you want to use this function

Then make a copy of node.tpl.php and rename it node--boatlisting.tpl.php (where "boatlisting" was the name of the content type.

Had a slight issue with an underscore in the content type's name (it was originally boat_listing) - I omitted to put in the underscore when I renamed the file, but changed the machine name to boatlisting so that it did not have any special characters. (I have a policy when coding to avoid special characters whenever possible - had used a space and Drupal auto-created an underscore - my bad - but that's another story).

SUCCESS!!

Next job: Styling the variables within the content types!!

bradallenfisher’s picture

If this code doesn't work

function THEME_preprocess_page(&$vars, $hook) {
  if (isset($vars['node']->type)) {
    // If the content type's machine name is "my_machine_name" the file
    // name will be "page--my-machine-name.tpl.php".
    $vars['theme_hook_suggestions'][] = 'page__' . $vars['node']->type;
  }
}

try this code

function THEME_preprocess_page(&$variables) {
  if (isset($variables['node']->type)) {
    // If the content type's machine name is "my_machine_name" the file
    // name will be "page--my-machine-name.tpl.php".
    $variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type;
  }
}

for omega 4 the argument is already set for you in the theme_page_preprocess function.

omzo’s picture

Thx, this helped me

kubilayrd’s picture

Thanks, that's a great solution. Really useful tip.

prsnjtbarman’s picture

This works for me, need to declare YourTheme_preprocess_page(&$vars, $hook)

peeterdog’s picture

Tnx

dapseen’s picture

Cool, it works

Thank you

sangam basnet’s picture

Hi,

Does this function work for drupal 8. Because when I tried adding this function to theme and followed everything until clearing the cache it didn't work. Do you have any idea for me please. I want custom template for content-types. I have followed most of google answers, it looks strange. I might be missing something.

I pretty much know I haven't missed to copy the code from google and naming the file name according and clearing the cache. 

Thank you.

freelance web designer’s picture

step one
---------
create a content type for example: my content type is "fullwidthpage"

step Two
------------

copy page.tpl.php and rename to page--fullwidthpage.tpl in template folder.

step Three
----------------

Add this code to template.php

function <strong>YOUR_THEME_NAME_HERE</strong>_preprocess_page(&$variables) {
  if (isset($variables['node']->type)) {
    // If the content type's machine name is "my_machine_name" the file
    // name will be "page--my-machine-name.tpl.php".
    $variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type;
  }
}

Don't forgot to change YOUR_THEME_NAME_HERE

ryan_neff’s picture

Thanks for this - it was really helpful!

johnhanley’s picture

My theme's template.php file already contained a hook_preprocess_page function so I simply needed to paste the above snippet. However the problem is my function parameter is named "$vars" instead of "$variables", which caused some temporary ("why isn't this working?!") confusion.

Hopefully this will save others a few minutes of time.

QuarQuar’s picture

Thank you so much! I spent like an hour copying in the various forms of that snippet into hook_preprocess_page function, reloading and clearing the cache, only eventually to read this post and realize that the $variables in the snippet didn't match the $vars in the function definition. Derp.

janu01’s picture

Hi,

We need to generate a form with html design in drupal 7. The form we created in content type with fields but we don't know to proceed with providing html designs to created fields.

We also tried with adding coding to template.php and overriding concepts but it was not working. Kindly support to proceed if do you have any samples.

Thank you.

wotney’s picture

Other modules might interfere, for instance "Display suite" which will override any custom templates you're adding in this manner.
You can set the content type to ignore any Display Suite settings by navigating to the "Manage display" section of that particular content type and setting "Select a layout" to "- None -"

swatichouhan012’s picture

function THEME_preprocess_page(&$variables) {
  if (isset($variables['node']->type)) {
   $variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type;
   }
} 

1)Add this code in your template.php.

2) create page--content_type_name.tpl.php in your template folder.
Note : dont forget to replace content_type_name.

hardik jayeshbhai hihoriya’s picture

This is what works for me:

In my theme folder I have a template.php file with the following code:

<?php
function sev_preprocess_page(&$variables) {
    if (isset($variables['node']->type)) {
        $nodetype = $variables['node']->type;
        $variables['theme_hook_suggestions'][] = 'page__' . $nodetype;
    }
}

Create a file 'page--yourcontenttype.tpl.php' in your templates folder (replace 'yourcontenttype' with something else).
Clear the cache and reload your page.

Good luck!

dalegrebey’s picture

Not working in Drupal 7 for some reason.

  • Content Type = Event;
  • page--event.tpl.php
  • Cleared Cache
function recordi_preprocess_page(&$variables) {

  // PAGE--CONTENT_TYPE.TPL.PHP
  if (isset($variables['node'])) {
    $variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type;
  }

}

Keeps using page.tpl.php instead of page--event.tpl.php

Any thoughts of anything else to check?

sangam basnet’s picture

I recognise that you have page--event.tpl.php. It is suppose to be page__event.tpl.php.

Either change inside function or change the file name to page__event.tpl.php.

Hope this will help :)