如何在Drupal之间和之间添加代码但排除某些节点

时间:2017-06-24 10:59:26

标签: drupal drupal-7 adsense

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

1 个答案:

答案 0 :(得分:0)

像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函数。

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

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);
  }
}