• Hey Guys,

    I currently have an adsense block in my wordpress header which appears on all pages of my blog. Im happy with this, but I want to exclude that ad section if a certain template is used, image.php.

    I have tried several if statements in the header to try and accomplish this, aswell as read over the codex in detail, but I cant work it out.

    What exactly do I need to use to exclude X if the image.php template file is currently being used please?

Viewing 12 replies - 1 through 12 (of 12 total)
  • <?php if (!is_page_template('image.php')) {?>
    
    Your ad section code
    
    <?php }?>

    Possibly?

    Thread Starter andymacdonalduk

    (@andymacdonalduk)

    Hey there,

    I havent actually tried it yet, but on first site, does that code only include ads on the image.php page?

    What I am looking for is the ads to be on every single page of the site, except the image.php template… if that makes sense?

    that’s what it does….ads on any page except the image.php template.

    (or at least that’s what it should do)

    Thread Starter andymacdonalduk

    (@andymacdonalduk)

    Hey there,

    If that’s the case, I must apologise for getting it wrong, however I have added the code to my header.php file, and it doesn’t seem to have done anything. The ads are still showing up on every page of the website, including when the image.php file is being used.

    Here is the code Im using…

    <?php if (!is_page_template('image.php')) {?>
    
    <div style="background:#F4F9F9; border:solid; border-width:1px; border-color:#A5A5A5;text-align:center;padding:10px; margin-bottom:15px;-moz-border-radius-:3px; -webkit-border-radius: 3px;">
    <script type="text/javascript"><!--
    google_ad_client = "pub-9907904751335716";
    /* 728x90, created 7/4/10 */
    google_ad_slot = "3185972388";
    google_ad_width = 728;
    google_ad_height = 90;
    //-->
    </script>
    <script type="text/javascript"
    src="https://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script></div>
    
    <?php }?>

    Have I done anything wrong? Just incase you need to look at the site in question, its https://www.purebeyonce.com

    You can go to this link, which will show you the image.php template in-use. As you can see, the leaderboard adsense block is still displayed along the top of the page, despite the if statement being placed in the header (inside the wrapper div).

    https://purebeyonce.com/beyonce-knowles-dangerously-in-love-photoshoot/beyonce-knowles-01-580×796

    Thread Starter andymacdonalduk

    (@andymacdonalduk)

    Can anybody help me with this please?

    <?php if (is_page_template('image.php')) { } else { ?>
    
    <div style="background:#F4F9F9; border:solid; border-width:1px; border-color:#A5A5A5;text-align:center;padding:10px; margin-bottom:15px;-moz-border-radius-:3px; -webkit-border-radius: 3px;">
    <script type="text/javascript"><!--
    google_ad_client = "pub-9907904751335716";
    /* 728x90, created 7/4/10 */
    google_ad_slot = "3185972388";
    google_ad_width = 728;
    google_ad_height = 90;
    //-->
    </script>
    <script type="text/javascript"
    src="https://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script></div>
    
    <?php } ?>

    maybe this? (sorry boss, this isn’t totally my strength, so I’m tryin to work through it too…it’s how I learn)

    Thread Starter andymacdonalduk

    (@andymacdonalduk)

    Hey no worries, I appreciate that there is somebody out there who is willing to help me, and even-more-so that like me, your also not an expert at it.

    I used the code you gave me, and again, it hasn’t made a difference. I have no idea why it wouldn’t be working because from the looks of it, its just a standard if statement – I have exactly the same code on other parts of my site which work perfectly.

    The only thing I can think of is that the header is not within the loop is it? Do If statements work outside of the loop? Im not sure?

    Thread Starter andymacdonalduk

    (@andymacdonalduk)

    What Im going to do Voodoo (don’t know your real name), is instead of including the adsense in the header, Ill just stick it inside a php file, and call it from within all of the template pages with a php include.

    That way, I can omit the code from the image.php by not calling that specific file. It seems like the easiest and quickest work around, and it’ll do the exact same job, so ill do that instead.

    Thanks very much for your help and advice though!!

    they do…. I use them outside of the loop….
    I’ve just never referenced one using a page template….

    I see references to global $post; for outside the loop, but that seems to be for a different situation….. it would go here in the conditional…

    <?php
    global $post;
    if (is_page_template('image.php')) { } else { ?>
    
    <div style="background:#F4F9F9; border:solid; border-width:1px; border-color:#A5A5A5;text-align:center;padding:10px; margin-bottom:15px;-moz-border-radius-:3px; -webkit-border-radius: 3px;">
    <script type="text/javascript"><!--
    google_ad_client = "pub-9907904751335716";
    /* 728x90, created 7/4/10 */
    google_ad_slot = "3185972388";
    google_ad_width = 728;
    google_ad_height = 90;
    //-->
    </script>
    <script type="text/javascript"
    src="https://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script></div>
    
    <?php } ?>

    but again, I think it’s not for this type of application

    Thread Starter andymacdonalduk

    (@andymacdonalduk)

    Yeah it’s good to know that you can use if statements outside the loop, i didn’t know that.

    I tried the code above with the global&post; as you mentioned, and the same thing happened, it continued to show up. But Im going to do what I mentioned in my previous post (before you left yours).

    Ill stick the ad code in a php file and call it from every page I need it to show up on. That way I can not call it from the image.php file and it wont show up. That seems like the easiest and quickest way to get the job done.

    yup, that’ll definitely work… hopefully somebody will come along and show what we weren’t getting right for future reference!

    Thread Starter andymacdonalduk

    (@andymacdonalduk)

    Yes, it would be interesting to know what the problem was, but in this case, I now have it working with the php includes, so im happy.

    Thanks again sir. ??

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Can an If Statement Work In the WordPress Header?’ is closed to new replies.