D7 Custom Module: Turning tabs into contextual links
Place the for loop inside an if statement

$tabs = menu_primary_local_tasks();
if($tabs)
{
      foreach( $tabs as $tab ){
          $element['#links'][$tab['#link']['path']] = array(
            'title' =&gt; $tab['#link']['title'],
            'href' =&gt; url($tab['#link']['href'], array('absolute' =&gt; TRUE)),
          );
      }
}