Adding ad code below X paragraphs?
-
I want to show adsense ads below the 4th and 20th paragraph of every blog post, ONLY if there is a 20th paragraph.
I managed to place the adsense below the 4th ad by placing the following code in my functions.php file, however I don’t know how to add an additional ad after the 20th.
//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 style="float:left;padding: 15px;"><script type="text/javascript"> google_ad_client = "ca-pub-xxxxxxxxxxxx"; google_ad_slot = "xxxxxxxxxx"; google_ad_width = 300; google_ad_height = 250; </script> <!-- Malben --> <script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js"> </script></div>'; if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $ad_code, 3, $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 ); }
Please assist, thank you ??
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Adding ad code below X paragraphs?’ is closed to new replies.