WordPress.org

Ready to get started?Download WordPress

Forums

Inserting adsense into posts via the loop (8 posts)

  1. uno
    Member
    Posted 7 years ago #

    I'm looking to insert adsense into the loop, but only for posts #1, 5, and 10. I have 10 posts per page, so I was looking for a way to get my adsense code into only these specific posts. Does anyone know code I can add to the loop so it calls the code IF the post on the page is one of these specific numbers in the loop for the page?

  2. iand
    Member
    Posted 7 years ago #

    Based on these two threads, give this a whirl:
    http://wordpress.org/support/topic/55981
    http://wordpress.org/support/topic/63693

    <?php if(($post == $posts[0]) || ($post == $posts[4]) || ($post == $posts[9])) {
    <div class="your ad div">
    Your ad code here
    </div>
    <?php } ?>

    You might need to adjust the 0,4 and 9 to suit, I am just guessing as the first post appears be considered number 0 for the loop. Backup, this might cause meltdown :) - (i have not tested it but I think it is sound)

    I think there also a guide for similar things here:
    http://www.tamba2.org.uk/wordpress

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Sure. Just outside the loop, above the while, add this code:
    <?php $post_counter=0; ?>
    Then, inside the loop, where you want the ad to be (anywhere before the "endwhile;" statement), add something like this:
    <?php
    $post_counter++;
    if ($post_counter == 1 || $post_counter == 5 || $post_counter == 10) { ?>
    <!-- insert adsense code here -->
    <? php } ?>

    This method is a bit more certain than relying on the $post or $posts variables, which may or may not be around depending on the theme and version and such. It's also faster, in some cases.

  4. uno
    Member
    Posted 7 years ago #

    The latter worked beautifully. THANK YOU!

  5. davidchait
    Member
    Posted 7 years ago #

    Note that my CG-Inbetween plugin was written to do exactly this: put 'something inbetween' given posts on the page. It can take numbered offsets (5 -- or even 1,5,10), 'last' (at the end of the page, regardless of number of posts...), 'single' (only on single pages, after the post). Manages the 'counter' for you with a single call to inbetweenPosts() added to your theme.

    -d

  6. bazil
    Member
    Posted 7 years ago #

    Otto - your method looks to be the more simple, but I am having a bit of difficulty adding that code. I am using the default template for testing and it fails to load the page at all. Is there something (other then the adsense code) I should be altering?

    Thanks

  7. bazil
    Member
    Posted 7 years ago #

    Hold that thought, I just used Ian's suggestion from:
    http://www.tamba2.org.uk/wordpress/adsense/

    it that worked fine. I am sure they both work in the same way. One day I will learn PHP and be the one answering questions..

    Thanks folks.

  8. davidchait
    Member
    Posted 7 years ago #

    Yeah, people who don't know PHP would be MUCH better served using plugins that do the 'heavy lifting' for them... ;) ;)

    -d

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags