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

3 Answers 3

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>";
            }
     ?>
0
function insert_custom_content($content) {
    $custom_content = '<div>Content to be inserted</div>';

    if (is_single() && ! is_admin()) {
        return insert_after_paragraph($custom_content, 2, $content);
    }

    return $content;
}
add_filter('the_content', 'insert_custom_content');

function 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);
}
1
  • 1
    Nice code. We are big fans of definitive answers here. If you were to explain why this code answers the question you answer might be a great fit. Commented Mar 10, 2021 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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