Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    You say that you have the ads inserted directly into the template. So what it sounds like you need is some way to flag a post and prevent the ads from displaying if that post is one that would be displayed on the page.

    You can flag a post using a custom field, or tags, or perhaps even by using keywords in the post content.

    Figuring out if that post will be displayed is trickier. Essentially, you need to run through the Loop and look for your flag in all the posts, then call rewind_posts() to rewind the loop and show the posts.

    So you use something like this in your header.php:

    <?php
    while (have_posts()) {
    the_post();
    // check for your flag here, somehow and set $adsense;
    // false means block the adsense, true means show it
    $adsense = false;
    }
    rewind_posts();
    ?>

    This is to set your flag. Then, everywhere you use adsense, you surround it with this:

    <?php if ($adsense) { ?>
    adsense code here
    <?php } // end adsense code ?>

    Then it won’t display unless you’ve set $adsense to true.

    Now, how do we check? Well, you could set a custom field on those posts. Say, add a “noadsense” key to them (with any value, doesn’t matter, “1” would work). Then your check in the header.php would be like this:

    <?php
    while (have_posts()) {
    the_post();
    $custom=get_post_custom();
    if (array_key_exists('noadsense',$custom)) {
    $adsense = false;
    break;
    }
    else $adsense=true;
    }
    rewind_posts();
    ?>

    And there you have it.

    Thread Starter stephennewton

    (@stephennewton)

    Perfect. Thank you very much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Block Google Adsense on individual posts’ is closed to new replies.