3

I want to use different node templates if the url is appended with ?template=x

Content type is called image

Default Node template using node--image.tpl.php:

  • blakedesign.co.nz/testsite/node/1

Same node with different template via URL possibly node--image--templateX.tpl.php:

  • blakedesign.co.nz/testsite/node/1?template=2
  • blakedesign.co.nz/testsite/node/1?template=3
  • blakedesign.co.nz/testsite/node/1?template=4

How do I go about doing this?

1 Answer 1

4

You can create custom template suggestions for node using hook_preprocess_node... In your case it should be below code in your theme template.php file... You can read query arguments using $_GET variable..

function YOURTHEME_preprocess_node(&$variables) {
  if ($variables['type'] == "image" && isset($_GET['template'])) {
    $variables['theme_hook_suggestion'] = 'node__image__template' . $_GET['template'];
  }
}

Your template file should be...

node--image--template1.tpl.php

node--image--template2.tpl.php

node--image--template3.tpl.php etc...
2
  • 1
    Works like a charm. Changed if ($variables['type'] == "image" && $_GET['template']) { to if ($variables['type'] == "image" && isset($_GET['template'])) { May 9, 2014 at 3:18
  • shouldn't it would be better if we use $variables['theme_hook_suggestion'][]
    – Ali Nouman
    May 11, 2014 at 15:03

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.