How php content after the first and second paragraph

I found a way to make a call to a get_template_part.

<?php 
$paragraphAfter[1] = '<div><?php get_template_part( "part-related", "ad-first" ); ?></div>'; //display after the first paragraph
$paragraphAfter[3] = '<div><?php get_template_part( "part-related", "ad-third" ); ?></div>'; //display after the third paragraph
$paragraphAfter[5] = '<div><?php get_template_part( "part-related", "ad-fifth" ); ?></div>'; //display after the fifth paragraph

$content = apply_filters( 'the_content', get_the_content() );
$content = explode("</p>", $content);
$count = count($content);
for ($i = 0; $i < $count; $i++ ) {
    if ( array_key_exists($i, $paragraphAfter) ) {
      $string = eval('?>'.$paragraphAfter[$i].'<?php;');    //  CLOSE PHP SINCE THE CHAIN ​​TO EVALUATE OPENS IT, AND THEN, OPEN PHP AS THE CHAIN ​​TO EVALUATE IT CLOSES IT
        echo $string;
    }
    echo $content[$i] . "</p>";
    }
?>

Leave a Comment