记录生活 学习SEO优化 | 他日若遂凌云志 敢笑黄巢不丈夫
>>Home > 学习笔记 > WordPress首页第X篇日志后显示广告

WordPress首页第X篇日志后显示广告

Tags: | | 作者:海天无影

好的博客,就算放上广告也还是很漂亮的,不像一些只为了赚钱的网站,完全不顾用户体验,铺天盖地的广告框,接二连三的弹窗。访客想要在页面上找需要的内容都要翻山越岭,差点都要带上放大镜去找那生存在广告夹缝中的内容。不过像那样的站一般都是电影站、小说站等,就算广告满天飞,照样还有访客进的站。

像我们的一般的草根站长可没那么牛,所以还是乖乖的放上少许广告,即不影响美观,又能稍微赚点外快。

在WordPress博客首页的第一篇或者第X篇日志后显示广告,这样即不会让访客感觉你的站点很乱,很多广告,又能在你指定的地方放上广告。当然也不一定是广告,在日志后放上图片、小贴士、通知等也是蛮有意思的。方法是在ShawnBlog看到

方法有两种,海天也会简单讲下原理。个人推荐第二个,简单,而且不需要重新创建变量。

第一种方法:

1、打开index.php文件,在 <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 前插入    <?php $postcnt = 1; ?>

2、在<?php the_content(); ?>,后加入代码:

<?php if ($postcnt == 1) : ?>
广告代码
<?php endif; $postcnt++; ?>

如果你还想把广告加在第二篇日志后,那就将第2步中的代码<?php if ($postcnt == 1) : ?> 改成 <?php if ($postcnt == 1 || $postcnt == 2) : ?> ,以此类推。

此方法的原理:在进入日志列表循环之前,创建一个计数变量postcnt并赋值为1,然后在下面循环输出日志的时候,每输出一篇日志摘要,技术变量postcnt就自加1,这样每篇日志就等于有各自的顺序数字了。然后在if需要的第X篇日志后显示广告。





第二种方法:

打开index.php文件,在<?php the_content(); ?>,后加入代码:

<?php if ($wp_query->current_post == 0) : ?>
广告代码
<?php endif; ?>

如果你还想把广告加在第二篇日志后,那就将代码中的$wp_query->current_post == 0 改成1,第X篇日志改成X-1,以此类推。

此方法的原理,由WordPress自带函数得到日志列表的日志计数,直接利用函数判断在需要的第X篇日志后显示广告。





希望大家的站点都能加上美观的广告,而不失你博客的档次,这里再送上其他两篇关于WordPress博客广告的文章:

投影样式的广告图片显示形式

WordPress针对特定访客显示广告

日志信息 »

该日志于2010-05-05 10:27由 海天无影 发表在学习笔记分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下转载到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

24条评论▼点击进行评论


  1. Warning: copy(/home/wwwroot/haitian/blog/avatar/0add08e212ec07498618635abac4850f.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    :mrgreen: 在你这里没有看到广告哦,呵呵没有加吗~^_^


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵是哦
      上次有说过,新博客,等过段时间在加的~


  2. Warning: copy(/home/wwwroot/haitian/blog/avatar/fad14a832ca37404b8e610f4179d0be0.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    WP没啥研究 :mrgreen:


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      额 才发现你不是WordPress的 呵呵~


  3. Warning: copy(/home/wwwroot/haitian/blog/avatar/152f90fdd68816e41c53dab837931b2a.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    我也学习,完了有了广告就会加了


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵 我广告都有
      百度联盟、GoogleAdsense都有
      只是这个是新博,等过段时间再加~


      • Warning: copy(/home/wwwroot/haitian/blog/avatar/152f90fdd68816e41c53dab837931b2a.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
        回复

        貌似能挣上钱,我也准备申请广告去。


  4. Warning: copy(/home/wwwroot/haitian/blog/avatar/0f6840987df0326df65aaa142fd9245b.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    我也不是wp的
    这个是新博 那就博的网址是什么呀


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵 旧博客是电影站,现在专门更新最新的大片了
      我是靠电影出名的啦~ :mrgreen:
      欢迎光临海天无影Movie:http://htwy.orgru


      • Warning: copy(/home/wwwroot/haitian/blog/avatar/d94967b298c3fcfed3f8e5a6c5181f42.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
        回复

        这样挺好,新博广告太多用户很反感的,我倒是无所谓 :lol:


        • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
          回复

          恩 这个博等过段时间
          放些美观的广告 哈哈~


          • Warning: copy(/home/wwwroot/haitian/blog/avatar/d94967b298c3fcfed3f8e5a6c5181f42.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
            回复

            到时候我给你点


            • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
              回复

              哈哈 欢迎欢迎~


  5. Warning: copy(/home/wwwroot/haitian/blog/avatar/c4668a51a1479f6c344c7b79636c2236.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    是的我喜欢第一句


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      嘿嘿 多谢捧场~


  6. Warning: copy(/home/wwwroot/haitian/blog/avatar/91c195394dd67967fae3f9d5ecb76046.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    新博什么时候加广告更合适呢?


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      额 说实在的,我也不知道
      反正博客人气上来了,收录量和搜索量都差不多了,这时候上博客应该差不多
      要不然没什么人气上了也没什么用啊~


  7. Warning: copy(/home/wwwroot/haitian/blog/avatar/c97d5ed7936e8a93bf668d771bc0eb00.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    记下了 ,以后用得着


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      嗯 以后加广告就用的着了
      个人博客,一定要美观 嘿嘿~


  8. Warning: copy(/home/wwwroot/haitian/blog/avatar/ae1d9cb02574d579039efc62a447f2f3.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    没看到你的广告呢


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵,我还没做的,等过段时间,人气稍微高点的时候就加了~


  9. Warning: copy(/home/wwwroot/haitian/blog/avatar/b041fec6d9b41851a84ef29a4827c1d7.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    这个不错,做个标记。我的那个站,基本上被我搞成个CMS了,汗,哈哈


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      CMS,这个广告方式用在CMS上不错的哦
      符合CMS加广告的风格,呵呵~


  10. Warning: copy(/home/wwwroot/haitian/blog/avatar/1aff72ea18f324abc5b6d0c8493a03e6.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    哈哈 加广告的原则是不影响用户体验
    前提是 能赚钱
    哈哈 需要平衡


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      额 加广告肯定都是想能小赚一点的了
      但是正如所说不能影响用户体验。


  11. Warning: copy(/home/wwwroot/haitian/blog/avatar/24fd7387366fc8d6b48c691dfa17abb2.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    我要加,应该就是每篇都加了,整的麻烦


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      每篇都加……你一个博客首页最起码不少于5篇日志
      你每篇都加,不说铺天盖地的广告影响美观,有碍用户体验
      而且google的广告一个页面不能多余3个广告位的……


  12. Warning: copy(/home/wwwroot/haitian/blog/avatar/8a696fb2b60f79867e39e7803303fc7a.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    相当实用,不过我看你的博客也没有放广告嘛!
    我的博客基本上没放过广告,Google的申请不理我,同时自己又嫌麻烦。
    所以广告就不了了之了。


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      呵呵 我是等再过段时间,等这个新博客人气稍微高点了再加的
      你的博客为什么google还不通过,时间也够了啊,你再好好填下资料多申请几次呢?
      我当时可能是因为老博客域名问题不给通过,我就用公司的大网站申请通过了,再给这些小站用,嘿嘿


  13. Warning: copy(/home/wwwroot/haitian/blog/avatar/8a696fb2b60f79867e39e7803303fc7a.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    老兄,我想问一下,你干嘛把评论者的URL给转了呢?
    难道就是为了避免一些商业宣传?这样也另可错杀一千了吧。


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      嘿嘿,宁可错杀一千,不能放过一个
      再说评论的url都是为了回访的
      朋友的链接 我每个页面都加了友链的呢
      话说你还没加友链呢,要加不?嘿嘿


      • Warning: copy(/home/wwwroot/haitian/blog/avatar/8a696fb2b60f79867e39e7803303fc7a.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
        回复

        不必不必,你那个电影博客上有我的友链,而我把你的友链改成你的这个博客了,有一个就行了,为你节省点空间吧。


        • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
          回复

          呵呵 随你啦
          改成我这个就更好了,以后有新电影,你就看这个BLOG侧栏的最新影视栏目吧,有新电影在movie更新,侧栏也会更新的~


  14. Warning: copy(/home/wwwroot/haitian/blog/avatar/e657612476777faf50d5f28627975291.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    这个方法用在其它博客上也可以的 :mrgreen:


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      第一种方法应该很多都能用,只要是循环输出的
      但第二种是WordPress的专有函数,应该只有WordPress一个能用的吧~


  15. Warning: copy(/home/wwwroot/haitian/blog/avatar/c7066dbe1db70fc3942d21503df04338.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    哎呀。这个方法很好,抛砖引玉了~~ :mrgreen:
    晚上回去捣鼓移接到其他功能~~THX~


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      嗯 是的
      正如文中所说“也不一定是广告,在日志后放上图片、小贴士、通知等也是蛮有意思的”


  16. Warning: copy(/home/wwwroot/haitian/blog/avatar/dd1635e688b764b024cfbead86871c54.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    采用第二种方法current_post == 0) : ?>
    广告代码

    这样只是第一篇有,如果我要第一篇和第三篇同时有呢?怎么处理?是同时


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      额 我的php不是很好
      要是给我的话
      我就多弄几个if……


      • Warning: copy(/home/wwwroot/haitian/blog/avatar/dd1635e688b764b024cfbead86871c54.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
        回复

        先试试


  17. Warning: copy(/home/wwwroot/haitian/blog/avatar/6cf34f76c40ef58af395d8d20069cc72.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    这个代码是能在首页看到的广告吗?


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      恩 是的 首页的


  18. Warning: copy(/home/wwwroot/haitian/blog/avatar/183e70c965b598c833f5dfb94fc4027b.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    The home loans seem to be very useful for guys, which would like to organize their career. In fact, that’s very easy to receive a sba loan.


  19. Warning: copy(/home/wwwroot/haitian/blog/avatar/b276d8963acd9fa462ef4aeb25545c79.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
    回复

    还是不会弄,可以申请友情链接吗


    • Warning: copy(/home/wwwroot/haitian/blog/avatar/3d58c5baea42043cdd0f012c22cfd774.jpg): failed to open stream: Permission denied in /home/wwwroot/haitian/blog/wp-content/themes/prowerV3.1/functions.php on line 169
      回复

      不好意思 才看到 可以~

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif 

« 五四青年节,大家放假了吗? 再现传奇 »