• Resolved Optic

    (@optic)


    Hello there,

    I am using WordPress attachment pages for my full image link views.

    Currently I have implemented Google AdSense by adding the code in attachment.php so it displays on all attachment pages.

    I would now like to exclude AdSense from displaying from some of the attachment pages. So perhaps an array of attachment IDs that I manually input in the template?

    Looking at the WP functions it looks like I should use function is_attachment ?

    Would something like this be the right approach?

    <?php
    
    $galleryExclusions = array(10000,10001,100002,20422);
    
    if (!is_attachment($galleryExclusions) ) {
        // Adsense Code here
    }
    
    ?>

    Any help would greatly be appreciated, thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Have you considered going down the plugin route? https://www.ads-software.com/plugins/ad-inserter/

    I think you would have to call all attachments as well as your excluded attachments otherwise its going to call it for all posts.

    if (is_attachment() && !is_attachment($exclusions))

    not tested.

    Thread Starter Optic

    (@optic)

    Just confirming the code I originally posted above works, will monitor and advise if any ill-effects.

    Andrew – I did consider a plugin and it is something I may consider later down the track – however currently my needs are very basic and am only inserting them in a few sections instead of every page.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Exclude AdSense on attachment page for a given set of IDs?’ is closed to new replies.