Google Adsense限制了每页最多出现三个Content单元
用Wordpress做的网站如果每篇文章内容的开头都加上了Google Adsense代码的话首页默认文章列表只会显示最多3条GGAD
其他文章前边虽然有了代码,但不会显示,这样就太浪费了~
很多人估计都用一些插件来实现~不过我不喜欢弄一堆插件之类的,感觉太臃肿,效率不高
还是自己动手的好些~
至少在首页加几个广告还是很简单的
首先找到你的WP所使用的模板目录的index.php文件
找到
复制内容到剪贴板
代码:
if(have_posts()):
这句
改成
复制内容到剪贴板
代码:
if(have_posts()):$tmpI = 0;
然后找到
复制内容到剪贴板
代码:
the_content(__('Read the rest of this entry »»'));
改为
复制内容到剪贴板
代码:
$tmpI ++;
If($tmpI<3){
the_content(__('Read the rest of this entry »»'));
}Else{
the_content_new(__('Read the rest of this entry »»'));
}
因为文章显示全部内容的单页调用文章内容也用的the_content函数,所以我在the_content里边加入了Google Adsense的代码~
the_content_new则是显示其他广告代码的函数了,这个是新加的
(当然你也可以不新加一个函数,而是修改the_content,把the_content函数扩充一个参数,根据参数来显示不同的广告~这里我只是简单的做一个演示就不那么做了)
the_content函数的位置是:wp-includes/post-template.php
原来的代码是:
复制内容到剪贴板
代码:
function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
}
这里我们把它修改为
复制内容到剪贴板
代码:
function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = "<div style=\"width: 300px; height: 250px; float: right;\">\r\n <script type=\"text/javascript\"><!--\r\n google_ad_client = \"pub-*************\";\r\n //Advert in content\r\n google_ad_slot = \"8466361530\";\r\n google_ad_width= 300;\r\n google_ad_height = 250;\r\n //--></script>\r\n <script type=\"text/javascript\" src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">\r\n</script></div>".$content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
}
然后加一个显示其他广告的函数
复制内容到剪贴板
代码:
function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = "<div style=\"width: 300px; height: 250px; float: right;\">\r\n "<div style=\"width: 300px; height: 250px; float: right;\">\r\n <!-- Begin: AdBrite -->\r\n<script type=\"text/javascript\">\r\n var AdBrite_Title_Color = '66CCCC';\r\n var AdBrite_Text_Color = '000000';\r\n var AdBrite_Background_Color = 'FFFFFF';\r\n var AdBrite_Border_Color = '66CCCC';\r\n</script>\r\n<script src=\"http://ads.adbrite.com/mb/text_group.php?sid=*****&zs=***********\" type=\"text/javascript\"></script>\r\n<div><a target=\"_top\" href=\"http://www.adbrite.com/mb/commerce/purchase_form.php?opid=****&afsid=1\" style=\"font-weight:bold;font-family:Arial;font-size:13px;\">Your Ad Here</a></div>\r\n<!-- End: AdBrite --></div>".$content;</div>".$content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
}
临时申请了个AdBrite来做实验~这里的广告代码可以自己换自己的~