Adding additional advert after set number of paragraph Adding additional advert after set number of paragraph wordpress wordpress

Adding additional advert after set number of paragraph


There is too much wrong with your ad code for me to attempt guessing what it should be (it has an opening <div> but no closing </div>, it has what appears to be javascript outside of a <script> tag)

...so I skip that part, and simply show how to insert another paragraph instead - this will insert something in the spots you want, and also shows how to use get_post_type() to ensure ads are only shown on posts:

add_filter( 'the_content', 'prefix_insert_post_ads' );function prefix_insert_post_ads( $content ) {    //The last condition here ensures that ads are only added to posts    if ( is_single() && !is_admin() && get_post_type() === 'post' ) {        return prefix_insert_ads( $content );    }    return $content;}function prefix_insert_ads( $content ) {    $closing_p = '</p>';    $paragraphs = explode( $closing_p, $content );    foreach ($paragraphs as $index => $paragraph) {        $paragraphs[$index] .= $closing_p;        if ( in_array($index, array(1, 5, 9)) ) {            //Replace the html here with a valid version of your ad code            $paragraphs[$index] .= '<p style="background:#f00">Ad goes here</p>';        }    }    return implode( '', $paragraphs );}


Check functions in the section of Conditional Tags Index from https://codex.wordpress.org/Function_Reference

if(!is_page()) {    // do your tricks}

There're also some other functions you may need like is_home(), is_front_page() and etc.