WordPress.org

Ready to get started?Download WordPress

Forums

Adsense after first post only (40 posts)

  1. kcomphlint
    Member
    Posted 9 years ago #

    If I wanted to display an adsense banner on the home page, but only after the most recent post, what would i need to wrap the adsense code in?

  2. TechGnome
    Moderator
    Posted 9 years ago #

    I did something similar once for some thing.....
    Here's how I did it:
    1) Open your index.php file. Look for some code similar to this:
    <?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
    This is the start of the WP-Loop.
    2) Just BEFORE that line, insert this code:
    <?php $showAdSense = True; ?>
    3) find this code:
    <?php endforeach; else: ?>
    This is the bottom of the loop.
    4) Just BEFORE that line, put this:
    < If ($showAdSense) { ?> ... stick the adsense code here ... <?php $showAdSense = False;} ?>
    Making the appropriate changes (ie, inserting the adsense code where noted.
    5) Save file.
    6) Sit back and enjoy.
    TG

  3. kcomphlint
    Member
    Posted 9 years ago #

    worked like a charm.
    had to put a ?php before If in the first line, but other than that...
    Thanks.

  4. BenSaunders
    Member
    Posted 9 years ago #

    Top stuff - I've just implemented this on my site. As Jeremy says, it needs an extra ?php at the start of 4).
    Thanks, TechGnome!

  5. lcf
    Member
    Posted 9 years ago #

    Thanks! That's what I am looking for! :-D

    By the way, I modify it abit to make the inserted code(adsense ads) displayed randomly in the loop.

    1. BEFORE <?php foreach ($posts as $post) : start_wp(); ?>:-

    <?php
    $counter = 1;
    $showed = false;
    $postperpage = 7;
    $showAdSense = rand(1, $postperpage);
    ?>

    2. BEFORE <?php endforeach; ?> :-

    <?php
    if(($counter==$showAdSense) && $showed==false) {
    $showed = true;
    ?>
    ADSENSE CODE HERE
    <?php
    //$showAdSense = False;
    } else {
    $counter++;
    }
    ?>

    See it in action: http://www.liewcf.com/wp

  6. Michael Adams (mdawaffe)
    Member
    Posted 9 years ago #

  7. luiscamino
    Member
    Posted 8 years ago #

    this isn't working for me in my wp1.5 blog. has anyone made it work with the latest version?

  8. MisterTut
    Member
    Posted 8 years ago #

    TechGnome's solution is working great for me, but is there a way to stipulate that the ad should appear after, say, the xth post as opposed to only after the first? (using x as a variable, here)

  9. jalenack
    Member
    Posted 8 years ago #

    sure thing...before the Loop:

    $postnum = 1;
    $showadsense = 7; // the post you want it to display with

    In the loop:

    <?php if ($postnum == $showadsense) { **Adsense Code here** } ?>

    And right before the end of the loop,
    $adsense++;

  10. MisterTut
    Member
    Posted 8 years ago #

    Hi Jalenack,

    When I try implementing your suggestion, I just get the error:
    " parse error, unexpected '<'"

    Perhaps I am putting it in the wrong place?
    Was your code an addition or replacement to TechGnomes?

    Thanks,

    -Tut

  11. jalenack
    Member
    Posted 8 years ago #

    Well, mine was somewhat of an addition to TechGnomes. Same instructions, different code. I'll just *copy & paste* his instructions with the necessary replacements:

    1) Open your index.php file. Look for some code similar to this:
    <?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
    This is the start of the WP-Loop.

    2) Just BEFORE that line, insert this code:
    <?php
    $postnum = 1;
    $showadsense = 7;
    ?>

    3) find this code:
    <?php endforeach; else: ?>
    This is the bottom of the loop.

    4) Just BEFORE that line, put this:
    <?php if ($postnum == $showadsense) { ?> ... stick the adsense code here ... <?php } $postnum++; ?>

    Making the appropriate changes (ie, inserting the adsense code where noted. the $showadsense variable can be changed from '7' to whatever post you want it to display after.

    5) Save file.

    6) Sit back and enjoy.

  12. jerm
    Member
    Posted 8 years ago #

    Thank you for this! I found it very useful as I like to "blend" my ads with the rest of my blog content! :)

  13. tomhanna
    Member
    Posted 8 years ago #

    How could this be edited to show the ad after 1st and 2nd or 1st, 2nd and 3rd posts? I tried tinkering and just broke things.

  14. pedmic
    Member
    Posted 8 years ago #

    what directory is the index.php file that needs to be edited?

  15. Mark (podz)
    Support Maven
    Posted 8 years ago #

    It's the index.php in your theme editor - could also be called 'main template'

  16. Beel
    Member
    Posted 8 years ago #

    I'd recommend learning a bit more about the vehicle which is WP before tinkering under the hood, assuming you know where the hood is ;-). Have you even read the "manual"? http://codex.wordpress.org/Main_Page
    You might find this portion helpful in your current situation:
    http://codex.wordpress.org/Site_Architecture_1.5

  17. tomhanna
    Member
    Posted 8 years ago #

    Yeah, Beel, I read the f*ing manual. I already know that this is the index.php in the Theme folder aka Main Template. I tried using an elseif statement and it didn't work. I tried adding a second iteration of this using <?php if ($postnum == 2...etc. and that didn't work. I tried adding a second variable called "showadsenseagain" and then `<?php if ($postnum == $showadsenseagain) ..." and that didn't work. I didn't see a damn thing in the Site Architecture of WordPress that really answered the question of how to modify this little routine to output the code a second time after the second post, but thanks anyway.

  18. Beel
    Member
    Posted 8 years ago #

    Testy aren't we? Check the thread a bit more closely and you might see my comment had nothing to do with you. Then you might check out David Chait's CG-Inbetween. That should work for you. There are also threads you could search for where we posted how to add counters, etc., to the code to do what you want (but David's plugin makes life easier).

  19. tomhanna
    Member
    Posted 8 years ago #

    Yeah, I guess so. I was the one that mentioned tinkering, so it looked like you were responding to me.

  20. jahfingers
    Member
    Posted 8 years ago #

    What about AdSense BEFORE first post only... anyone know how that would work?

  21. tomhanna
    Member
    Posted 8 years ago #

    Exactly the same except you'd put the code before the_content instead of after the_content.

  22. jahfingers
    Member
    Posted 8 years ago #

    nevermind, justed placed AdSense code above the loop, under the content div

  23. ollerwe
    Member
    Posted 8 years ago #

    Very valuable information, thanks!

    I wonder if it is possible to display an image or a banner instead of adsense...

    /Erico

  24. tomhanna
    Member
    Posted 8 years ago #

    Yeah. It's also possible to display other types of ads like Adbrite or Yahoo Publisher Network. Instead of Adsense code, think "Advertising code".

  25. ollerwe
    Member
    Posted 8 years ago #

    It works very well. Thanks!
    /erico

  26. aprilia
    Member
    Posted 8 years ago #

    This works great! Is there a way to put a </br> immediately after the adsense block? Mine looks a little cramped.

    http://motorcyclebloggers.com

  27. Nghia
    Member
    Posted 8 years ago #

    guys,

    i cannot find this line anywhere in the index.php
    I have 1.5.2, is it normal?

  28. Heat
    Member
    Posted 8 years ago #

    I can't find the LOOP line, i'm using Blix..and it seems that the loop is either in another file or written in another, anyone can help?

    Heat

  29. froinlaven
    Member
    Posted 8 years ago #

    I'm using the latest wordpress and Blix too. The code for the php seems depricated or something. Try this: Before "<?php while (have_posts()) : the_post(); ?>" insert

    <?php $showAdSense = True; ?>

    Now, before "<?php endwhile; ?>" insert
    <?php if ($showAdSense): ?>
    your adsense code
    <?php $showAdSense = False; ?>
    <?php endif; ?>

    That should work. Hope this helps.

  30. janjalani
    Member
    Posted 8 years ago #

    what if you like it to show on 1st, 2nd and 3rd post?

Topic Closed

This topic has been closed to new replies.

About this Topic