• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Agent Press Pro: Adsense on posts only.

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

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

This topic is: resolved
  • This topic has 3 replies, 2 voices, and was last updated 10 years, 11 months ago by Brad Dalton.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • July 1, 2014 at 1:26 pm #112493
    tigaruk
    Member

    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 );
    }
    
    
    July 1, 2014 at 11:34 pm #112567
    Brad Dalton
    Participant

    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'))


    Tutorials for StudioPress Themes.

    July 2, 2014 at 8:51 am #112637
    tigaruk
    Member

    Code is from here.

    http://www.wpbeginner.com/wp-tutorials/how-to-insert-ads-within-your-post-content-in-wordpress/

    Where do i include that conditional tag?

    July 2, 2014 at 9:49 pm #112767
    Brad Dalton
    Participant

    Add it with the others.

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


    Tutorials for StudioPress Themes.

  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2025 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble