Drupal 6 page.tpl.php template based on path

| 0 comments

Use custom page.tpl.php files based on the URL path alias of your pages to create custom template files. Add the following code to your theme’s template.php file.

function phptemplate_engine_preprocess_page(&$variables) {
$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$variables['template_files'][] = $template_filename;
}
}
}

Create a copy of page.tpl.php and rename it to page-YOURPATH.tpl.php and begin to make your own custom edits specific to that path. Now, if you’ve got a www.yoursite.com/blog path, then Drupal will pick up page-blog.tpl.php as the template for that page. The above code is for Drupal 6. You may need to clear Drupal’s cache after you’ve done this for it to take effect.

Share

Author: Peter

Peter is a tech obsessed web designer and developer from Vancouver, BC Canada. With over 6 years of professional experience working on website design and development projects of all shapes and sizes, he has worked on everything from large multi-year government-funded web projects to lean and nimble small business websites. He's been operating a freelance web design and development company called Def Design since 2005. You can also find his web portfolio at www.webforaliving.com

Leave a Reply

Required fields are marked *.

*