Hello everybody,

I'm using a Zen subtheme that's working just fine. Now I'd like to use custom page templates, but for nodes with URL aliases, as described here http://drupal.org/node/139766

I can't seem to make this work with a subtheme. In my subtheme's template.php, I'm using the snippet from the URL above and trying to combine that with a snippet from the Page_Title module.

My pages load, but the Page_Title module no longer works and only my subtheme's page.tpl.php file is recognized. My various "custom" templates (page-urlalias.tpl.php) don't seem to be detected. Here is my code from my subtheme's template.php.

function mysubtheme_preprocess_page($hook, $vars = array()) {
  switch ($hook) {
    case 'page':
      // Add page template suggestions based on the aliased path.
      // For instance, if the current page has an alias of about/history/early,
      // we'll have templates of:
      // page-about-history-early.tpl.php
      // page-about-history.tpl.php
      // page-about.tpl.php
      // Whichever is found first is the one that will be used.
      if (module_exists('path')) {
        $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
        if ($alias != $_GET['q']) {
          $suggestions = array();
          $template_filename = 'page';
          foreach (explode('/', $alias) as $path_part) {
            $template_filename = $template_filename . '-' . $path_part;
            $suggestions[] = $template_filename;
          }
        }
        $vars['template_files'] = $suggestions;
      }
      break;
  }
	//Hide titles from appearing with the Page_Title module
    if (module_exists('page_title')) {
    $vars['head_title'] = page_title_page_get_title();
     }

  return $vars;
}

So, I'm sure I've messed something up in setting this up, or in combining the two snippets (I don't know PHP well).

I'd really appreciate any help you can offer. Thanks!

Comments

jkestler’s picture

Priority: Normal » Critical

I've now been working on this for a few days and still have not come up with a solution. I've tried removing the Page_Title code entirely, but I still can't get my subtheme to recognize anything but my page.tpl.php file.

Has anyone used different template files successfully with URL (path) aliases in a Zen subtheme? If so, any advice would be greatly appreciated.

Thanks!

jkestler’s picture

Priority: Critical » Normal
Anonymous’s picture

This snippet you described on drupal.org/node/139766 worked for me in my Zen subtheme once I made the following changes:

Delete the start of the code:

function mysubtheme_preprocess_page($hook, $vars = array()) {
  switch ($hook) {
    case 'page':

and the code at the end

     }
      break;
  }
  return $vars;
}

Let me know if you need any more info on it.

manorius’s picture

Hi creativesteele,

I've tried your suggestion but this is still not working
Here is the code that I'm using in the template.php file:

function NAMEofSUBTHEME_preprocess_page(&$vars) {


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

I want to theme all aliases that start with domain_name/news/
I'm using the page-news.tpl.php file to do that.

Any ideas why this is not working?

manorius’s picture

Status: Active » Closed (fixed)

I found a solution... I used the Drupal 6 code although I'm using Drupal 5.
So if you use the ZEN theme implement this code instead of the D5 code.
and which you can find here: http://drupal.org/node/139766

<?php
function phptemplate_preprocess_page(&$vars) {
  if (module_exists('path')) {
    $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
    if ($alias != $_GET['q']) {
      $suggestions = array();
      $template_filename = 'page';
      foreach (explode('/', $alias) as $path_part) {
        $template_filename = $template_filename . '-' . $path_part;
        $suggestions[] = $template_filename;
      }
    }
    $vars['template_files'] = $suggestions;
  }
}
?>