将脚本添加到Drupal 7上的某些节点和页面

时间:2019-05-21 18:46:55

标签: php drupal drupal-7

我试图在我的drupal的所有页面上添加脚本代码,除了少数几个。我可以在html.tpl.php中使用任何条件吗?或template.php上的任何功能来实现这一目标?

我在template.php上尝试了以下代码,但没有运气:(

提前谢谢!

 function THEME_preprocess_page(&$variables) {

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

  //this is what I am trying
  if ($variables['nid'] == '77') {
   drupal_add_js(drupal_get_path('theme', 'THEME') .'/scripts/path.js');
  }
} 

1 个答案:

答案 0 :(得分:4)

您可以使用hook_page_build(&$page)

function MYMODULE_page_build(&$page){

  $nids = array(123, 1234); // your nids 

  if(($node = menu_get_object('node', 1)) && in_array($node->nid ,$nids) ){
      drupal_add_js(drupal_get_path('theme', 'mythemename') .'/scripts/path.js');
  }
}

创建此函数后清除所有缓存以查看结果,并确保路径脚本正确;)