WordPress 在 more 截断处插入广告

前端之家收集整理的这篇文章主要介绍了WordPress 在 more 截断处插入广告前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在网站的文章内插入的广告具有相当高的点击率。在 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/

猜你在找的wordpress相关文章