11

I'm working on a Drupal 7 website. I created custom content type for works. I have many fields, and some of them are related to page layout like particular page bg for each entry. So I need custom page.tpl for this particular content type.

So I activated and opened Theme Developer module, and clicked the body area to get candidate name for this particular content type page entry. and it gives me the screen below which doesn't mean anything to me :/ When I click the node area, it is fine, it gives node--work.tpl.php, but I need for page. I tried "page--work.tpl.php", it didnt work.

What am I missing?

enter image description here

migrated from stackoverflow.com Jan 31 '12 at 15:48

This question came from our site for professional and enthusiast programmers.

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

Assuming your machine-readable content type name is "work", page--work.tpl.php will work.

Refer to this thread for more info.

  • wow! it worked great! I can address to page--work.tpl.php now :) although it works fine, I have an error message as d.pr/EaMa I googled and tried to solve, still trying... any idea? thanks a lot for your time!! – Drupal guy Mar 4 '11 at 13:49
  • oh I guess it was because of Theme Developer module. I disable the module and error has gone :) thanks a lot! – Drupal guy Mar 4 '11 at 14:01
2

In template.php add:

function themeName_preprocess_page(&$vars) {
  if (isset($vars['node'])) {
    // If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
    $vars['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--',    $vars['node']->type);
  }
}

You can omit str_replace(); test what version works best for you.

Then, create a template file whose filename is page–-[content_type].tpl.php. (Replace [content_type] with the content type machine name.)

See http://drupal.org/node/1089656#comment-4426790.

1

In case you want the node id as well, use this.

function mytheme_process_page(&$variables) {
  if (isset($variables['node'])) { 
    $variables['theme_hook_suggestions'][] = 'page__type__'. $variables['node']->type;
    $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->nid;
  }
}
0

Having a different template for each content type is not a good practice according the guys at Drupal. That is the reason you don't find page--work.tpl.php in the candidate template file list. They feel only the node template files should be different. However if you want to hack in, go ahead.

Use path alias to set up a path based on the content type. And then insert this code

 if (module_exists('path')) {
    $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
    if ($alias != $_GET['q']) {
      $template_filename = 'page';
      foreach (explode('/', $alias) as $path_part) {
        $template_filename = $template_filename . '-' . $path_part;
        $vars['template_files'][] = $template_filename;
      }
    }
  }

into the phptemplate_preprocess_page(&$vars) function in template.php in the theme folder.

You can modify the code a little to make it more efficient though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.