0

You have used the code of this question (Show content after the first and second paragraph) and it works correctly for me.

    <?php
$paragraphAfter[1] = '<div>AFTER FIRST</div>'; //display after the first paragraph
$paragraphAfter[3] = '<div>AFTER THIRD</div>'; //display after the third paragraph
$paragraphAfter[5] = '<div>AFTER 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) ) {
        echo $paragraphAfter[$i];
    }
    echo $content[$i] . "</p>";
} ?>

I've been watching and remixing. But I have not found the way to add a call type "get_template_part". I do not recognize the part of get_template_part. Where is the error, or can not be done?

Change this:

$paragraphAfter[1] = '<div>AFTER FIRST</div>'; 

For this:

$paragraphAfter[1] = '<div> get_template_part( 'ad-first' );</div>'; 

I have tried to make a simple echo, but there is something that I fail

$paragraphAfter[1] = '<div> echo "Hello world!"; </div>'; 

I can not get it to work :-( Any help or guidance is welcome, thanks

0

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>";
    }
?>
0

I think this is an improved version of the previous one:

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


            $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]);    // Eval string
                    echo $string;
                }
                echo $content[$i] . "</p>";
            }
     ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.