Agent Press Pro: Adsense on posts only.

Community Forums Forums Design Tips and Tricks Agent Press Pro: Adsense on posts only.

This topic is: resolved

This topic contains 3 replies, has 2 voices, and was last updated by  braddalton 3 months, 3 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #112493

    tigaruk
    Participant
    Post count: 18

    http://casuallyhardcore.com/testsite/product/test-shirt-6-2/

    Im using the code below to add an adsense advert under the first paragraph on my posts. However it’s also adding the advert to my product pages in woocommerce as seen in the above link. How do i disable it adding to these pages and only on posts. Happy to totally change the code if theres something better.

    
    //Insert ads after second paragraph of single post content.
    
    add_filter( 'the_content', 'prefix_insert_post_ads' );
    
    function prefix_insert_post_ads( $content ) {
    	
    	$ad_code = '<div id="googleads"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <!-- Casually Hardcore Side Bar Top -->
    <ins class="adsbygoogle"
         style="display:inline-block;width:336px;height:280px"
         data-ad-client="ca-pub-5783869104156427"
         data-ad-slot="3373941828"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script> </div>';
    
    	if ( is_single() && ! is_admin() ) {
    		return prefix_insert_after_paragraph( $ad_code, 1, $content );
    	}
    	
    	return $content;
    }
     
    // Parent Function that makes the magic happen
     
    function prefix_insert_after_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 );
    }
    
    
    #112567

    braddalton
    Participant
    Post count: 10258

    Where did you get the code from? Please link to the source. Thanks.

    You can exclude WooCommerce pages using a conditional tag

    ! is_singular(‘product’))


    #112637

    tigaruk
    Participant
    Post count: 18
    #112767

    braddalton
    Participant
    Post count: 10258

    Add it with the others.

    if ( is_single() && ! is_admin() ) {


Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.