Previously, to add an ad code after the account identified </h2> used the following code

 add_filter('the_content', 'wpse_ad_content'); function wpse_ad_content($content) { if (!is_single()) return $content; $paragraphAfter = 2; //Enter number of paragraphs to display ad after. $content = explode("</h2>", $content); $new_content = ''; for ($i = 0; $i < count($content); $i++) { if ($i == $paragraphAfter) { $new_content.= '<div style="margin-bottom: 10px;">'; $new_content.= 'Рекламный блок'; $new_content.= '</div>'; } $new_content.= $content[$i] . "</h2>"; } return $new_content; } 

Now you need to add the code after the closing </div> certain class. Each entry has a block to share from Yandex, it is wrapped in <div class="ya-share2"> after this closing </div> and you want to add the code.

  • What prevents you from simply adding this code in the template in which you display the Yandex block? - alenkins
  • The fact is that the share buttons were added manually to each article separately (i.e. they are in the "body" of the article) - c3589668

1 answer 1

If there is one ya-share2 on the page and there is no other </div> between <div class="ya-share2"> and the desired </div> , then it should help:

 add_filter('the_content', 'wpse_ad_content'); function wpse_ad_content($content) { if (!is_single()) return $content; $new_content = '<div style="margin-bottom: 10px;">'; $new_content.= 'Рекламный блок'; $new_content.= '</div>'; $content = explode("ya-share2", $content, 2); $add_content = explode("</div>", $content[1], 2); $add_content[1] = $new_content.$add_content[1]; $new_content = $content[0] . "ya-share2" . $add_content[0]."</div>" . $add_content[1]; return $new_content; } 
  • Thank you very much. It helped. - c3589668