为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
绝地无双 2021-06-30 18:35:34
我试图在我的 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 回答

?
森林海

TA贡献2011条经验 获得超2个赞

您可以使用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');

  }

}

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


查看完整回答
反对 回复 2021-07-02
  • 1 回答
  • 0 关注
  • 133 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信