AshleyQuick Posted October 17, 2017 Share Posted October 17, 2017 Is it possible to modify this code to add an additional ad div? The existing is inserted after the 3rd paragraph. I'd like one more that is inserted after the 6th. <?php //Insert ads after third paragraph of single post content. add_filter( 'the_content', 'prefix_insert_post_ads' ); function prefix_insert_post_ads( $content ) { $ad_code = '<div>Ads code goes here</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 ); } Quote Link to comment https://forums.phpfreaks.com/topic/305385-in-article-ad-code-for-wordpress-would-like-to-modify-this-script-to-add-an-additional-ad/ Share on other sites More sharing options...
phpmillion Posted October 17, 2017 Share Posted October 17, 2017 (edited) Did you try calling prefix_insert_after_paragraph function once more? Like prefix_insert_after_paragraph( $ad_code, 6, $content ); Edited October 17, 2017 by phpmillion Quote Link to comment https://forums.phpfreaks.com/topic/305385-in-article-ad-code-for-wordpress-would-like-to-modify-this-script-to-add-an-additional-ad/#findComment-1552779 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.