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?


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--template3.tpl.php etc...
|improve this answer|||||
  • 1
    Works like a charm. Changed if ($variables['type'] == "image" && $_GET['template']) { to if ($variables['type'] == "image" && isset($_GET['template'])) { – blakedesign May 9 '14 at 3:18
  • shouldn't it would be better if we use $variables['theme_hook_suggestion'][] – Ali Nouman May 11 '14 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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