0
我試圖將Google AdSense'網頁級廣告'添加到我的Drupal網站。它需要在兩個標籤<head>
和</head>
之間粘貼一些代碼。然而,我想要從中排除一些節點。由於它們都是相同的節點類型,它們將從相同的page.tpl.php文件加載。我該怎麼做呢?謝謝。如何在Drupal中添加<head>和</head>之間的代碼,但不包括某些節點
我試圖將Google AdSense'網頁級廣告'添加到我的Drupal網站。它需要在兩個標籤<head>
和</head>
之間粘貼一些代碼。然而,我想要從中排除一些節點。由於它們都是相同的節點類型,它們將從相同的page.tpl.php文件加載。我該怎麼做呢?謝謝。如何在Drupal中添加<head>和</head>之間的代碼,但不包括某些節點
像Drupal中的所有東西一樣,有很多方法可以做到這一點。您可以檢查預處理函數中的特定節點標識,然後只在頁面呈現特定節點標識時添加該代碼。
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');
}
}
爲了使人們更易於管理,我會做的就是添加該節點類型複選框場爲「無廣告」什麼的。這將爲您提供從管理界面刪除該節點的廣告的過程,您不需要不斷修改代碼以排除某些節點標識。
現在製作(或添加)您的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代碼一起可以解決問題。謝謝! – alec