1

I'm trying to create a completely different template for a page I have set up at:

http://www.example.com/my_page

I thought I just added a new node tpl page (node-my_page.tpl.php), but that doesn't appear to be working. What's the trick?

  • You can use a template like node-[nid].tpl.php, where [nid] is the node id. – ovi Jan 13 '12 at 21:53
1

node-my_page.tpl.php wont work without additional code. page-my_page.tpl.php will in case my_page is the system path (node and page templates are not the same thing though). If it's an alias you should find out the system path and create the template file using that. For example if the system path is node/5 you'd create page-node-5.tpl.php.

If simple template suggestions aren't quite enough you can use the code snippets I added below to make things work (copy to your theme template file).

Different page template:

function mytheme_preprocess_page(&$variables) {
  if (arg(0) == 'my_page') {
    $variables['template_file'] = 'page-my_page';
  }
}

Different node (content part of a page) template:

function mytheme_preprocess_node(&$variables) {
  if (arg(0) == 'my_page') {
    $variables['template_file'] = 'node-my_page';
  }
}

Note that if my_page is not the system path, but only the alias you should use the system path (node/5 in this example) instead:

if ($_GET['q'] == 'node/5') {
  // ...
}

Or let Drupal get the alias for you:

$alias = drupal_get_path_alias($_GET['q']);
if ($alias == 'my_page') {
  // ...
}

You can find more information and additional examples here.

3

What you're looking for are template suggestions. If you want to change the page layout (regions, general html) use template suggestions for page.tpl.php. OTOH, if you want to change the node layout (the .node div and whatever is within), use template suggestion for node.tpl.php.

  • Yea, I saw that, but what if I want to change a template based on a URL? I don't see that mentioned anywhere. – kylex Jan 13 '12 at 21:11
  • page-[front|internal/path].tpl.php the internal/path is the url. Do you want to change the node layout or the page layout? – Capi Etheriel Jan 13 '12 at 21:50
  • The node layout. It needs a completely different look. New css, new layout, everything. – kylex Jan 13 '12 at 21:58
  • try node-nid.tpl.php. – Capi Etheriel Jan 14 '12 at 0:31

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.