在網站的文章內插入的廣告具有相當高的點擊率。在 WordPress 里,我發現很多人利用 JavaScript 把廣告插入到 more 截斷標簽處,作為內文廣告。昨晚我也在內文里放了 Google Adsense,但我是用 WordPress 自帶的 add_filter 函數實現的。
打開主題的 function.php ,插入下面的代碼:
/**
* The filter to insert the ads
*/
function bl_insert_ad_code_filter( $content ) {
global $id;
// 只在文章頁面顯示
if ( !is_single() ) {
return $content;
}
// first, get the code to insert
$html = '<div class="single_ads">你的廣告代碼</div>';
// more 標簽在 WordPress 2.3 前是一個 a 標簽,2.3 后是一個 span 標簽
// 保證兼容性
return preg_replace("#< (a|span) id="more-$id">#", $html."$0", $content, 1);
return $content;
}
add_filter('the_content', 'bl_insert_ad_code_filter', 50);
利用這個 filter 我們還可以在文章任意的地方插入廣告,或者添加其他的應用,大家可以盡情發揮創意。
原文:http://blog.imbolo.com/wordpress-insert-a-advertisement-at-the-more-tag/