[Resolved] Problem with script to remove adsense from posts/pages

Home Forums Support [Resolved] Problem with script to remove adsense from posts/pages

Home Forums Support Problem with script to remove adsense from posts/pages

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #648505
    Alessio

    Good morning,

    we are trying to NOT display adsense on some posts and pages, because of the google adsense policy.
    I found a script, which works fine when we try to exclude only one post, but it creates problems when we add a comma and more post id’s

    the problems we find are: now the categories pages are empty (no content) and the wordpress toolbar does not appear when we browse our sites while logged in. if we exclude just one post, it all works fine, if we add more than one (see below) the problem comes back

    we added the following script to the footer hook

    <?php
    $currentpageid = get_the_ID();
    If ($currentpageid != 64,7440,2443) {
    echo “<script async src=\”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\”></script>”;
    }
    ?>

    all our adsense tags are without the pagead2.googlesyndication.com/pagead/js/adsbygoogle.js script, so the tags should not work only on posta and pages where they cannot be displayed

    please if you can, we would appreciate your help in understanding where the problem is
    thank you in advance
    alex

    GeneratePress 2.1.3
    GP Premium 1.7
    #648585
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Try this:

    $exclude = array( 64, 7440, 2443 );
    
    if ( ! is_page( $exclude ) ) {
        // Stuff in here is excluded.
    }
    #648718
    Alessio

    hi tom,
    thank you first of all.

    I tried to replace my (working when using only one page number) script with the one you gave me, but it gives exactly the same problem I am having with the original script – in the case of the new code the problem happens even when using just one post number (wordpress toolbar not diplayed and categories pages empty)

    I apologize if maybe I was not able to understand your suggestion, this is how I tried to implement it

    I added to the wp_footer hook the following

    <?php
    $exclude = array( 62, 7640, 2143 );
    if ( ! is_page( $exclude ) ) {
    // Stuff in here is excluded.
    <script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>
    }
    ?>

    And enabled Execute PHP

    But it does not work.

    If you have any other advice on how to exclude adsense only from specific posts and pages, I will be glad to hear. In the meantime I will try to find alternative options, and if I find a solution I will write it on this page

    Thank you again, your theme is amazing and support from your team is really top

    Best regards
    Alex

    #649475
    Tom
    Lead Developer
    Lead Developer

    Try using this code instead:

    <?php
    $exclude = array( 62, 7640, 2143 );
    if ( ! is_page( $exclude ) ) {
        ?>
        // Stuff in here is excluded.
        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
        <?php 
    } 
    ?>

    Alternatively you can use our new Elements module: https://docs.generatepress.com/article/hooks-element-overview/

    It has Display Rules so you can ditch the PHP completely.

    #650008
    Alessio

    Hi Tom

    The new Elements is definitely powerful, and within one minute the problem was fixed. No need for PHP, just use elements.

    Thank you for your support and for the GREAT theme, really appreciated

    Alex

    #650023
    Tom
    Lead Developer
    Lead Developer

    That’s great to hear! Thank you 🙂

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.