1

I'm having some trouble trying to put an if else statement into my "page.tpl.php". I don't remember (I did it in the past), how to check something like that, but with some other page for example "www.example.com/home-2" or www.example.com/info . In my case is a page view, I would really appreciate any help you can provide.

1

Well if I understand you right...

In template.php

function mytheme_preprocess_page(&$variables) {
  $node = menu_get_object('node');

  if (isset($node) {
    $variables['node_type'] = $node->type;
  }
}

In page.tpl.php

<?php if ($node_type == 'your_type') : ?>
  // something here for node type your_type
<?php endif; ?>

Additionally, you can simply add new template suggestions.

In template.php

function mytheme_preprocess_page(&$variables) {
  $node = menu_get_object('node'); 

  if (isset($node)) {
    $variables['node_type'] = $node->type;
    $variables['theme_hook_suggestions'][] = 'page__node__' . $node->type;
  }
}

After clearing your cache, you can create page--node--(node_type).tpl.php and put page specific markup for that node type there.

0

Yes, the first two anwsers did the trick.

I knew that i can do the theme_suggestions, but i didn't want to create more files so the menu_get_object worked like charm.

PD: I did it before but i couldn't remember how (i deleted those files).

THANKS!!!

EDIT: The solution in my case was to put the

  if (isset($node)) {
    $variables['node_type'] = $node->type;
  }

into the template.php

and then put my custom code with a conditional (for example):

<?php if ($is_front || $node_type == 'info_list'): ?>

into the page.tpl.php

I hope it helps somebody.

  • Okay cool. Mark as accepted if that is your solution. – Kevin Jan 12 '17 at 18:21
  • If you combine the two answer to get your solution you can edit this answer with the full description of how do you solve your problem, is better for other users – Adrian Cid Almaguer Jan 12 '17 at 18:39

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.