Created
June 13, 2016 18:25
-
-
Save anonymous/5168bc997f88eda9775989f76736c2d2 to your computer and use it in GitHub Desktop.
Wie Du Werbung in WordPress nach dem xx Absatz einbaust
This file contains hidden or 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 | |
| /** | |
| * Werbeblock nach dem zweiten Absatz einfuegen | |
| * | |
| */ | |
| add_filter( 'the_content', 'tb_insert_post_ads' ); | |
| function tb_insert_post_ads( $content ) { | |
| $ad_code = 'Hier kommt der Werbe-Code hinein. Entweder statische Werbung oder auch Google Adsense.'; | |
| if ( is_single() && ! is_admin() ) { | |
| // Die Zahl vor Content bestimmt, wo der Code erscheint. Hier nach dem zweiten Absatz eines Artikels. | |
| return tb_insert_after_paragraph( $ad_code, 2, $content ); | |
| } | |
| return $content; | |
| } | |
| // Parent Function that makes the magic happen | |
| function tb_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 ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment