Guest User

Untitled

a guest
Jan 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. <?php
  2. //Insert ads after second paragraph of single post content.
  3. add_filter( 'the_content', 'prefix_insert_post_ads' );
  4. function prefix_insert_post_ads( $content ) {
  5. $ad_code = '<div>Code của bạn tại đây</div>';
  6. if ( is_single() && ! is_admin() ) {
  7. return prefix_insert_after_paragraph( $ad_code, 2, $content );
  8. }
  9. return $content;
  10. }
  11. // Parent Function that makes the magic happen
  12. function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
  13. $closing_p = '</p>';
  14. $paragraphs = explode( $closing_p, $content );
  15. foreach ($paragraphs as $index => $paragraph) {
  16. if ( trim( $paragraph ) ) {
  17. $paragraphs[$index] .= $closing_p;
  18. }
  19. if ( $paragraph_id == $index + 1 ) {
  20. $paragraphs[$index] .= $insertion;
  21. }
  22. }
  23. return implode( '', $paragraphs );
  24. }
Add Comment
Please, Sign In to add comment