Add code after X paragraphs, without counting paragraphs inside <blockqoute>

The function bellow add ads between paragraphs. I’m trying to make it to count a blockqoute as a single paragraph, regardless of how many paragraphs are inside that blockqoute.

I wasn’t able to find the implementation for this. Can anyone provide me a bit of help? I suspect that the hint is to use the DOMDocument() method, but i’m new to php and i don’t know exactly how to do it.

add_filter( 'the_content', 'add_ads_to_content' );

function add_ads_to_content( $content ) {

    $ads = array(
        2 =>  'ad code 1', // paragraph_id => ad_code
        4 => 'ad code 2', // paragraph_id => ad_code
        6 => 'ad code 3' // paragraph_id => ad_code
    );

    if ( is_single() && ! is_admin() ) {
        foreach ($ads as $paragraph_id => $ad_code) {
            $content = prefix_insert_after_paragraph( $ad_code, $paragraph_id, $content );
        }
    }

    return $content;
}

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 );
}

Well, you would do that when you create the list of paragraphs. You must be pulling them from somewhere.
Just set up a counter variable and if it equals 3, set it back to 0 and add the AD in.

You would need to do that in the code that displays your paragraphs. Make sense?

It makes sense, but can you offer me an example or something like that?

This function should be called when you create the display of the content’s paragraphs. You did not show that code. Let’s say you read the content paragraphs from a database and display them one after another. That would be the place to handle your counter and then call this routine to add the AD in.
I can’t give you an example because you did not show where you display the paragraph’s. I guess I can fake something like this for you:

//  Display content paragraphs
$paragraph_counter = 0;
while($paragraphs = mysqli_fetch($results)) {
    echo ($paragraphs['paragraph_text'];
    $paragraph_counter++;     //  increment counter by 1
    if($paragraph_counter=3} {
        $paragraph_counter=0;    //  reset counter
        add_ads_to_content;
    }
}

The function to add ADs to the content does not need to be a function. You can just do it in the counter area instead. It’s not really the type of code that needs to be a function. Functions usually would be for pages where you need to call the function more than once. In this case, you can just place the ads when you display the content and therefore is only done when the content is displayed.

I understand now. Thanks!

Glad to help. That is why we are here…

Sponsor our Newsletter | Privacy Policy | Terms of Service