WordPress的more处插入广告

在文章内插入的广告具有相当高的点击率。在 WordPress 里,我发现很多人利用 JavaScript 把广告插入到 more 截断标签处,作为内文广告。昨晚我也在内文里放了 Google Adsense,但我是用 WordPress 自带的 add_filter 函数实现的。

打开主题的 function.php ,插入下面的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * 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);
版权所有:为网站而疯狂-给站长一个五星级的家转载请注明来源,谢谢!

已有 1 条评论       我也不甘寂寞

  1. 踏浪者/2010-12-30 11:27

    学习了,不错的方法 回击

    #1

我看不过去了,也来两句 返回»  

友情提示:
1、请勿发表色情、违法、商业广告等信息,谢谢。
2、留言想要有头像?请看 “我” 的。