Drupal 7: custom template based on url alias


/ Published in: PHP
Save to your folder(s)

This will give you the ability to create custom node templates based on the URL alias of a page.

Example: if you create a page with the URL alias "aboutus", you can now use node--page-aboutus.tpl.php as your node template.


Copy this code and paste it in your HTML
  1. function template_preprocess_node(&$variables) {
  2. if(isset($variables['node']) && $variables['type'] == "page") {
  3. $path = drupal_get_path_alias($variables['path']);
  4.  
  5. $variables['theme_hook_suggestions'][] = 'node__page_' . $path;
  6. }
  7. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.