2017-06-24 35 views

回答

0

像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); 
    } 
} 
+0

這將很好地工作。我剛剛發現Drupal Google Adsense模塊具有很大的靈活性。與Adsense代碼一起可以解決問題。謝謝! – alec

相關問題