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 ); } Link to comment Share on other sites More sharing options...
phpmillion Posted October 17, 2017 Share Posted October 17, 2017 Did you try calling prefix_insert_after_paragraph function once more? Like prefix_insert_after_paragraph( $ad_code, 6, $content ); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.