要在 WordPress 文章中插入廣告,要在文章一開始插入或文章結尾插入都很簡單。可以查看之前怎樣在WordPress文章中加入廣告的說明。但如果要在文章中間或其他任何位置插入廣告時該怎辦?

這時就可以考慮在後台的編輯器中新增個廣告按鈕。這裡以Google AdSense來舉例。假設我們需要在HTML編輯器里加上一個Google AdSense廣告的插入按鈕。首先需要編寫一段關於Google AdSense的短代碼並加入到funtions.php文件裡。下面是一個Google AdSense段代碼的示例:

function do_adsense() {
  $adsense = "你的AdSense代碼";
  return $adsense;
}
add_shortcode('adsense', 'do_adsense'); 

完成上面的代碼後,我們需要在wp-includes/js資料夾下的quicktags.dev.js文件裡加入代碼:

edButtons[edButtons.length] = 
new edButton(‘ed_adsense' 
,'adsense' 
,'[adsense]' 
,' ' 
,'adsense' 
); 

然後將quicktags.dev.js更名為quicktags.js,替換原有的quicktags.js文件。(為防止意外情況發生,請事先備份這兩個檔)

保存並刷新後台的編輯介面,你就會看到新的編輯按鈕了,當然你也可以稍微改動其中的代碼來添加其他廣告按鈕。

以後,寫文章時,想插入廣告,就按一下這個廣告按鈕就輕鬆完成了。

同時,以後要變更廣告內容時,也不用一個個文章去修改了,只要修改funtions.php廣告碼就好了;有人會問,如果要在文章內任意位置加入不同的廣告要怎做呢?舉一反三阿,您可以在funtions.php內加了一個do_adsense(),當然可以再加do_adsense1()do_adsense2().....同樣的quicktags.dev.js要加的內容也可以如法泡製。

相关文章