Title: Disabling Adsense on specific posts
Last modified: October 17, 2017

---

# Disabling Adsense on specific posts

 *  [satrap](https://wordpress.org/support/users/satrap/)
 * (@satrap)
 * [8 years, 6 months ago](https://wordpress.org/support/topic/disabling-adsense-on-specific-posts/)
 * Hi,
 * I am trying to disable Adsense on one specific post on my site (on a Custom Genesis
   theme).
 * Now I am displaying 2 Adsense units in each post via a custom function file.
 * The first one goes under the title and original code was this:
 *     ```
       // Lets Insert Adsense under title
       add_action( 'genesis_entry_content', 'insert_adsense_under_title', 2 );
       function insert_adsense_under_title() {
       ?>
       <?php if(is_single): ?>  
       Adsense Code Here
       <?php endif; // is_single() ?>
       <?php
       }
       ```
   
 * I was able to disable the ad on that specific post by changing `<?php if(is_single):?
   >`
 * to this:
 *     ```
       <?php if(is_single('post-slug')): ?>  
       <?php else: ?>
       ```
   
 * Essentially, that (‘post-slug’) stops the ad from being displayed in that specific
   post which the slug belongs to.
 * I have to do the same with the second ad block.
 * But with the other ad, I am using a different code that displays the ad under
   nt paragraph (after paragraph 16) of the post.
 * This is the code:
 *     ```
       // Lets Insert First Adsense after nth paragraph of each post
       add_filter( 'the_content', 'prefix_insert_first_adsense' );
       function prefix_insert_first_adsense( $content ) {
       	$ad_code = '<div>
       Adsense code here
       </div>';
       	if ( is_single() && ! is_admin() ) {
   
       		return prefix_insert_first_adsense_after_the_paragraph( $ad_code, 16, $content );
       	}
       	return $content;
       }
       // Parent Function that makes the magic happen
       function prefix_insert_first_adsense_after_the_paragraph( $insertion, $paragraph_id, $content ) {
       	$closing_p = '</p>';
       	$paragraphs = explode( $closing_p, $content );
       	foreach ($paragraphs as $index => $paragraph) {
       		if ( trim( $paragraph ) ) {
       			$paragraphs[$index] .= $closing_p;
       		}
       		if ( $paragraph_id == $index + 1 ) {
       			$paragraphs[$index] .= $insertion;
       		}
       	}
       	return implode( '', $paragraphs );
       }
       ```
   
 * The method I used for the first ad doesn’t work here and I can’t figure out how
   to do it.
 * I would appreciate any help. Thank you very much in advance.
 * Satrap
    -  This topic was modified 8 years, 6 months ago by [satrap](https://wordpress.org/support/users/satrap/).
    -  This topic was modified 8 years, 6 months ago by [satrap](https://wordpress.org/support/users/satrap/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [egf](https://wordpress.org/support/users/egf/)
 * (@egf)
 * [8 years, 6 months ago](https://wordpress.org/support/topic/disabling-adsense-on-specific-posts/#post-9641382)
 * Did you try something like:
 *     ```
       if(is_single('post-slug')):  
       else:
           $ad_code = '<div>
           Adsense code here
           </div>';
       endif;
       ```
   
 * Which should leave your $ad_code empty if you are on ‘post-slug’.
    -  This reply was modified 8 years, 6 months ago by [egf](https://wordpress.org/support/users/egf/).
    -  This reply was modified 8 years, 6 months ago by [egf](https://wordpress.org/support/users/egf/).
 *  [Thomas Maier](https://wordpress.org/support/users/webzunft/)
 * (@webzunft)
 * [8 years, 6 months ago](https://wordpress.org/support/topic/disabling-adsense-on-specific-posts/#post-9641395)
 * Hi Satrap,
 * if you just want to make use of the Genesis hooks, then you could also choose
   [Genesis Ads](https://wordpress.org/plugins/advanced-ads-genesis/). It is an 
   add-on for Advanced Ads which allows you to target specific pages and posts on
   which to display the ad.
 * Thomas

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Disabling Adsense on specific posts’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 3 participants
 * Last reply from: [Thomas Maier](https://wordpress.org/support/users/webzunft/)
 * Last activity: [8 years, 6 months ago](https://wordpress.org/support/topic/disabling-adsense-on-specific-posts/#post-9641395)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
