php - How to inject code in posts after X images in Wordpress -


we have function in wordpress show block of code after 3 paragraphs in posts:

add_filter('the_content', 'wpse_ad_content');  function wpse_ad_content($content) {     if (!is_single()) return $content;     $paragraphafter = 3; //enter number of paragraphs display ad after.     $content = explode("</p>", $content);     $new_content = '';     ($i = 0; $i < count($content); $i++) {         if ($i == $paragraphafter) {             $new_content.= '<div style="col-xs-12">';             $new_content.= 'code here';             $new_content.= '</div>';         }          $new_content.= $content[$i] . "</p>";     }      return $new_content; } 

we trying complete function include , display block of code in post contains @ least 8 images (understanding image each

<img src=... 

).

this block should displayed after image nº8, if aren't 8 images, additional function doesn't display nothing.

----- edit 1: after trying @timtroiano solution

add_filter('the_content', 'wpse_ad_content');  function wpse_ad_content($content) {     if (!is_single()) return $content;     $paragraphafter = 3; //enter number of paragraphs display ad after.     $imagesafter = 7; //enter number of images display code snippet after.     $content = explode("</p>", $content);     $new_content = '';     ($i = 0; $i < count($content); $i++) {         if ($i == $paragraphafter) {             $new_content.= '<div class="col-lg-12" style="text-align: center; padding-top: 10px; margin-bottom: 20px;">';             $new_content.= '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>          <!-- la nube sobre título -->          <ins class="adsbygoogle"             style="display:block"             data-ad-client=""             data-ad-slot="7588478595"             data-ad-format="auto"></ins>          <script>             (adsbygoogle = window.adsbygoogle || []).push({});          </script>';             $new_content.= '</div>';         }          $new_content.= $content[$i] . "</p>";          if ($imagesafter > 0) {             $imagesafter -= 1;         } else {             $new_content.= '<div class="col-lg-12" style="text-align: center; padding-top: 10px; margin-bottom: 20px;">';             $new_content.= '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- la_nube_4_anuncio --> <ins class="adsbygoogle"     style="display:block"     data-ad-client=""     data-ad-slot="3663644595"     data-ad-format="auto"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>';             $new_content.= '</div>';             $imagesafter = 7; //reset number of images display code after.         }     }      return $new_content; } 

we noticed strange behavior. 1. consider paragraphs of text if pictures . 2. in addition repeats block of code if more images or paragraphs ( multiples of 8) , should appear once.

thank posting relevant code. added variable $imagesafter count how many images have been inserted before adding code snippet. when $imagesafter reaches 0 inserts whatever choose insert , resets $imagesafter original number.

what added:

$imagesafter = 7; //enter number of images display code snippet after. if ($imagesafter > 0) {     $imagesafter -= 1; } else {     **code snippet after set number of images goes here**     $imagesafter = 7; //reset number of images display code after. } 

here full code part added in appropriate places:

function wpse_ad_content($content) {     if (!is_single()) return $content;     $paragraphafter = 3; //enter number of paragraphs display ad after.     $imagesafter = 7; //enter number of images display code snippet after.     $content = explode("</p>", $content);     $new_content = '';     ($i = 0; $i < count($content); $i++) {         if ($i == $paragraphafter) {             $new_content.= '<div style="col-xs-12">';             $new_content.= 'code here';             $new_content.= '</div>';         }          $new_content.= $content[$i] . "</p>";          if ($imagesafter > 0) {             $imagesafter -= 1;         } else {             **code snippet after set number of images goes here**             $imagesafter = 7; //reset number of images display code after.         }     }      return $new_content; } 

here's simpler solution. use modulus operator test if $i multiple of 8, show image.

$if ($i > 0) {     if ($i % 8 == 0) {         **code snippet after set number of images goes here**     } } 

this go here in code:

function wpse_ad_content($content) {     if (!is_single()) return $content;     $paragraphafter = 3; //enter number of paragraphs display ad after.     $content = explode("</p>", $content);     $new_content = '';     ($i = 0; $i < count($content); $i++) {         if ($i == $paragraphafter) {             $new_content.= '<div style="col-xs-12">';             $new_content.= 'code here';             $new_content.= '</div>';         }         $if ($i > 0) {             if ($i % 8 == 0) {                 **code snippet after set number of images goes here**             }         }          $new_content.= $content[$i] . "</p>";     }      return $new_content; } 

let me know if solves problem or if looking different.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -