• 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 6 years, 6 months ago by satrap.
    • This topic was modified 6 years, 6 months ago by satrap.
Viewing 2 replies - 1 through 2 (of 2 total)
  • 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 6 years, 5 months ago by egf.
    • This reply was modified 6 years, 5 months ago by egf.

    Hi Satrap,

    if you just want to make use of the Genesis hooks, then you could also choose Genesis Ads. 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.