如何在Drupal之间<head>和</head>在Drupal中添加代码,但排除一些节点



我正在尝试将Google Adsense'页面级广告添加到我的Drupal网站。它需要在两个标签<head></head>之间粘贴一些代码。但是,我想从中排除一些节点。由于它们都是相同的节点类型,因此它们将从同一page.tpl.php文件加载。我该怎么做呢?谢谢。

喜欢Drupal中的所有内容,有很多方法可以做到这一点。您可以在预处理功能中检查特定的节点ID,然后在页面呈现特定节点ID时仅添加该代码。

function themename_preprocess_page(&$vars) {
  //check and see if we're rendering a node and if the current nid is in the a
  if (isset($variables['node'] && in_array($variables['node']->nid, array('1','2','3','4','5')) {
 drupal_add_js('your adsense code here',
    array('type' => 'inline', 'scope' => 'header');
  }
}

为了使人们更易于管理,我要做的就是为"无广告"或其他内容添加该节点类型的复选框字段。这将为您提供一个从管理界面中删除该节点的广告的过程,并且您不需要不断修改代码即可排除某些节点ID。

现在制作(或添加到(themename_preprocess_page函数。

//psuedo代码 - 不要复制和粘贴

theme_preprocess_page(&$vars) {
if ((isset($variables['node']->type) && $variables['node']->type == 'your_node_type') && isset($variables['node']->field_checkbox[LANGUAGE_NONE][0]) && $variables['node']->field_checkbox[LANGUAGE_NONE][0][value]) {
    drupal_add_js(your javascript ad code);
  }
}