GeneratePress

Conditional display of Google Ads or other content

Get the latest from
Sniffle Valve

Sniffle Valve

Consider buying Sniffle Valve a coffee  

Book a one-on-one online training session

Note: To copy any code presented in these tutorials, hover over the code and select the Toggle RAW Code icon raw code icon

Sometimes you may want to display Google Ads or other content on certain pages but not on others. For example, one generally does not display ads on the contact, disclaimer, privacy policy, and terms and conditions pages. Maybe the home page as well.

That’s where some conditional PHP comes in along with GP Hooks, part of the GeneratePress Premium add on.

In the first example, the responsive Google Ad will appear on all pages except the Home, Contact, Disclaimer, Privacy Policy, and Terms and Conditions pages. Both examples use the page slug in the array. Place the code in the desired GP Hooks location.

The second example shows an AnyWhere Elementor template shortcode.

Remember to tick the Execute PHP checkbox.

Note: the links in this post are NOT affiliate links. However, the items in the right sidebar are 🙂

<?php if ( ! is_page( array( 'home', 'contact', 'disclaimer', 'privacy-policy', 'terms-and-conditions' ) ) ) : ?>
    <div id="contentad"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Responsive Ad -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-xxxxxxxxxxxx"
     data-ad-slot="yyyyyyyyyyyy"
     data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<?php endif; ?>
<?php if ( ! is_page( array( 'home', 'contact', 'disclaimer', 'privacy-policy', 'terms-and-conditions' ) ) ) : ?>
    <?php echo do_shortcode('[INSERT_ELEMENTOR id="3248"]'); ?>
<?php endif; ?>

Leave a Comment

Your email address will not be published. Required fields are marked *