php - 将脚本添加到 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');
}
}
解决方案
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');
}
}
创建此函数后清除所有缓存以查看结果,并确保路径脚本正确;)
推荐阅读
- reactjs - 我可以在 react native 中设置导入组件的状态吗?
- vhdl - 如何将数据从一个进程传输到另一个进程?
- python - 如何使用networkx绘制线性图
- c# - 将文本转换为拉丁语编码并解码越南语的问题
- c - 如何在c中创建带有随机数的地图
- ruby-on-rails - Rails/Rspec - 与交互之前不存在的对象
- python - Bash 操作员错误:气流中没有这样的文件或目录
- node.js - 如何获取特定的用户数组(mongodb,nodejs)
- node.js - 在 URL 中指定端口 80 与未指定端口的结果不同
- android - 当设备仍处于开启状态时,BLE BluetoothGattCallback 会获得 STATE_DISCONNECTED