CRUSO Posted October 16, 2021 Share Posted October 16, 2021 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 ); } Quote Link to comment https://forums.phpfreaks.com/topic/314018-add-code-after-x-paragraphs-without-counting-paragraphs-inside/ Share on other sites More sharing options...
requinix Posted October 16, 2021 Share Posted October 16, 2021 Did you try what we said to do in your original thread? Quote Link to comment https://forums.phpfreaks.com/topic/314018-add-code-after-x-paragraphs-without-counting-paragraphs-inside/#findComment-1591139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.