Last active
August 29, 2015 13:59
-
-
Save andornagy/10664779 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // ignore this | |
function 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 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // ignore this | |
add_filter( 'the_content', 'insert_post_ads' ); | |
function insert_post_ads( $content ) { | |
// inster your code between the ' ' | |
$ad_code = '<div>Ads code goes here</div>'; | |
if ( is_single() && ! is_admin() ) { | |
/* | |
* 2 = the number of the paragraph | |
*/ | |
return insert_after_paragraph( $ad_code, 2, $content ); | |
} | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment