Alex
Alex

Reputation: 413

Notice: Undefined variable: node in include() Drupal 7

I have looked but it seems I can't find the right answer or I don't have the skills for that. The thing is I'm getting this error:

Notice: Undefined variable: node in include() (line 69 of /home/xwebmedia/public_html/ltr/sites/all/themes/ltr/page.tpl.php).

and the code I'm using is:

<?php
if (count($node->field_adds) != 0)
{
    foreach($node->field_adds['und'] as $key => $value)
    {               
        $nid = $value['nid']; 
        $mywidget = node_view(node_load($nid));
        print drupal_render($mywidget);
    }       
}
?>

Thing is everything is working fine, I get my widgets with adds in sidebar, but I am having this notice of error.

I know that I need to define a variable in template.php but I tried something it didn't work.

Any suggestions?

Upvotes: 0

Views: 4519

Answers (2)

Clive
Clive

Reputation: 36957

The page template file doesn't have a $node variable by default (you can have pages that aren't nodes so it's not required).

The menu_get_object() function is your friend here:

$node = menu_get_object();
if ($node) {
  ...
}

Upvotes: 0

Menztrual
Menztrual

Reputation: 41587

Check that $node has been set.

<?php
if (isset($node) && count($node->field_adds) != 0)
{
    foreach($node->field_adds['und'] as $key => $value)
    {               
        $nid = $value['nid']; 
        $mywidget = node_view(node_load($nid));
        print drupal_render($mywidget);
    }       
}
?>

Upvotes: 2

Related Questions