【问题标题】:How to add code between <head> and </head> in Drupal but exclude some nodes如何在 Drupal 中的 <head> 和 </head> 之间添加代码但排除某些节点
【发布时间】:2017-06-25 05:39:26
【问题描述】:

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

【问题讨论】:

    标签: drupal drupal-7 adsense


    【解决方案1】:

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

    【讨论】:

    • 这会很好用。我刚刚发现 Drupal Google Adsense 模块具有很大的灵活性。结合 Adsense 代码可以解决问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    相关资源
    最近更新 更多