Set-up
I have the following standard Adsense script inserted in my header.php
file,
<script async src="//pagead2.googlesyndication/pagead/js/adsbygoogle.js">
</script>
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-********",
enable_page_level_ads: true
});
</script>
This allows me to display ads on all pages instantly without too much hassle.
Problem
I'd like to prevent display of all adsense ads on specific pages of my website.
I'm looking for some 'if' function to stop the display. Anyone knows of such a function?
Set-up
I have the following standard Adsense script inserted in my header.php
file,
<script async src="//pagead2.googlesyndication/pagead/js/adsbygoogle.js">
</script>
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-********",
enable_page_level_ads: true
});
</script>
This allows me to display ads on all pages instantly without too much hassle.
Problem
I'd like to prevent display of all adsense ads on specific pages of my website.
I'm looking for some 'if' function to stop the display. Anyone knows of such a function?
Share Improve this question asked Jul 13, 2017 at 8:52 LucSpanLucSpan 1131 silver badge5 bronze badges 4 |3 Answers
Reset to default 2You can use this function:
<?php if(is_page('your-page-slug')): ?>
//do nothing on selected pages
<?php else: ?>
<script async src="//pagead2.googlesyndication/pagead/js/adsbygoogle.js" />
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-********",
enable_page_level_ads: true
});
</script>
<?php endif; ?>
If you want to disable this on more than one page, just add them like this:
<?php if(is_page('your-page-slug1') || is_page('your-page-slug2')): ?>
Wordpress Developer Resources are very useful (more than codex), so i recommend checking them out first if you don't know how to do something
Step 1: Login to your your AdSense dashboard and click on My ads>>Auto ads>>New URL group
Step 2: Select URLs The next thing you need to do is to enter all the URLs you want to choose a new global setting for…click on ‘Add URL’.
Step 3: Select ad settings for new URL groups Since you don’t want ads displaying on those pages, toggle off all ads format for those pages and click on ‘next‘.
Source: https://www.naijahomebased/stop-google-ads-from-displaying/
I can recommend the free plugin Advanced Ads for this mission. You can easily select the pages on which you want to (not) inject this code. Installation and setup will take around 10 minutes, but afterward, you can add pages without any need to touch code.
Here is a tutorial about this setup with Advanced Ads: https://wpadvancedads/how-to-block-ads-on-a-specific-page/
$dont_display = array(1,15,2106);
, only display your code, if current id is not one of thoseif ( ! in_array($post_id, $dont_display)) { /* your html code here */}
– kero Commented Jul 13, 2017 at 9:21