0
votes

I'm trying to add Google AdSense 'page level ads' to my Drupal website. It requires pasting some code between the two tags <head> and </head>. I want to however exclude some nodes from this. Since they are all the same node type, they will load from the same page.tpl.php file. How do I do this? Thanks.

1

1 Answers

0
votes

Like everything in Drupal, there are many ways to do this. You could check for specific node ids in a preprocess function and then only add that code when the page is rendering specific node ids.

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

To make it more manageable for people, what I would do is add a checkbox field for that node type for "No ads" or something. This will give you a process for removing ads from that node from the admin interface and you won't need to constantly tinker with code to exclude certain node ids.

Now make (or add to) your themename_preprocess_page function.

//psuedo code -- don't copy and paste

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