Adding an additional announcement after the set paragraph number

I currently have my site set up where it automatically adds a Google Adsense ad after the second paragraph of any article, but I would like to improve this if anyone can help.

I would like to add this code to add 2 more declarations; one after the sixth paragraph and one after the 10th. If the article does not reach these paragraph numbers, then the ads should not be displayed.

Maybe something is really obvious, but everything I've tried caused the functions.php file to crash on site reload.

My code ...

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {

$ad_code = '<div class="mobilead .visible-xs-block hidden-sm hidden-md hidden-lg"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle"
 style="display:block"
 data-ad-client="ca-pub-XXXX"
 data-ad-slot="1716361890"
 data-ad-format="auto"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>';

if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $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 );
}

      

As a follow-up question, is there a way to limit these ads to only show on posts and not pages? They are currently shown anywhere.

Any help would be greatly appreciated.

+3


source to share


2 answers


There are too many errors with your ad code to try and guess what it should be (it has an open <div>

but does not close </div>

), it has what appears to be javascript outside of the tag <script>

)

... so I'll skip that part and just show you how to insert another p

aragraph instead - this will insert something in the spots you want, and also shows you how to use get_post_type()

to make sure ads only show 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 );
}

      

+4


source


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

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

      



There are other functions that you may need, such as is_home()

, is_front_page()

etc.

0


source







All Articles