Nov19
0
Share

N 个 WordPress 小技巧(二)

今天带来的是两个和广告有关的技巧:

1、对搜索引擎带来的用户显示 AdSense 广告

通常老用户对于博客广告的布局非常了解,不会带来太多的广告点击,而搜索引擎带来的用户往往更容易点击广告。下面这个小技巧就是教用户如何只对搜索引擎带来的用户显示广告。

将下面这段代码写入 functions.php 文件,如果没有的话自己仿造其它的主题建一个。

function scratch99_fromasearchengine(){
 
$ref = $_SERVER['HTTP_REFERER'];
 
$SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.');
 
foreach ($SE as $source) {
    
if (strpos($ref,$source)!==false) return true;
 
}
 
return false;
}

其中 $SE 变量定义用户来自的搜索引擎,你可以自己添加,用逗号隔开。接着你只需将下面的代码拷贝到广告要出现的位置。

if (function_exists('scratch99_fromasearchengine')) {
 
if (scratch99_fromasearchengine()) {
    
AdSense Code
 
}
}

2、在 RSS Feed 中插入广告

将下面一段代码插入 functions.php 文件。

<?php
function insertAds($content) {
    
$content = $content.'<hr /><a href="http://www.sanliangfan.com">Put your ad code here</a><hr />';
    
return $content;
}
add_filter('the_excerpt_rss', 'insertAds');
add_filter('the_content_rss', 'insertAds');
?>

使用的时候只需在 insertAds 中添加需要添加的广告即可。

Filed under: | Tagged: ,

Leave a Reply