1

I'm using Drupal (version 7) for the first time and I have two content types, Landing Page and Article Page, each with their own templates page--landing_page.tpl.php and article_page.tpl.php respectively. The only reason I have two content types is because each content type needs to be displayed differently on the website.

I have several contents of the type Landing Page which I would like to display with the page--article_page.tpl.php template. So now I have the troublesome task of manually converting each of these content types to Article Page.

Is there a more effective way for a user to select the template he wants to use for a content instead of changing content types?

1 Answer 1

1

I ended up using "template suggestions" feature of drupal 7. Now I only have one content type called Article Page. I added a custom field to it called field_template, which takes the name of the template i want to use not including the page-- prefix and not including the .tpl.php suffix. So if field_template has the value 'landingpage', then the template will be 'page--landingpage.tpl.php';

Then in my ~/sites/all/themes/THEMENAME/template.php file, I wrote the following function

function THEMENAME_process_page(&$variables) {
  if (array_key_exists('node', $variables))
  {
        if($variables['node']->field_template['en'][0]['value'])
        {
        $variables['theme_hook_suggestions'] = array('page__'.$variables['node']->field_template['en'][0]['value']);
        }
        else
        $variables['theme_hook_suggestions'][] = 'page__'. $variables['node']->type;
  }
}

Replace THEMENAME with the name of your theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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