• Resolved CRE8

    (@cre8gr)


    Hey guys!

    Great plugin to help us covert our posts to amp pages!

    One question I have is that in my current site now I have a code inserted in my functions.php file to insert ads inside the post content after second and sixth paragraph. It looks like this:

    add_filter( 'the_content', 'prefix_insert_post_ads' );
    
    function prefix_insert_post_ads( $content ) {
    
    	$ad_code = '<div class="text-banner-ad" id="text-banner-ad">';
    	$ad_code = 'here is the ad code';
    	$ad_code .= '</div>';
    
    	if ( is_single()) {
    		return prefix_insert_after_paragraph( $ad_code, 2, $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 );
    }

    Is it possible some way to have this code inside the post content in amp pages? We use DFP so we will include the right tags for AMP ads.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Insert Ads inside content’ is closed to new replies.