• 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?

Viewing 15 replies - 1 through 15 (of 39 total)
  • 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

    Thread Starter kcomphlint

    (@kcomphlint)

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

    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!

    Thanks! That’s what I am looking for! πŸ˜€

    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

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

    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)

    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++;

    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

    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.

    Thank you for this! I found it very useful as I like to “blend” my ads with the rest of my blog content! πŸ™‚

    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.

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

    It’s the index.php in your theme editor – could also be called ‘main template’

    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

Viewing 15 replies - 1 through 15 (of 39 total)
  • The topic ‘Adsense after first post only’ is closed to new replies.