3

The function below is used to show certain content after the first paragraph. I would like to show 'content X' after the 1st paragraph and 'content Y' after 2nd paragraph.

<?php
$paragraphAfter= 1; //display after the first paragraph
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i++ ) {
if ($i == $paragraphAfter) { ?>

<div>Insert content here</div>

<?php }
echo $content[$i] . "</p>";
} ?>

I appreciate any help.

4 Answers 4

9

My way to do this (see update below):

function addParagraphs($content) {
    // you can add as many as you want:
    $additions = array(
        '<p>After 1st paragraph</p>',
        '<p>After 2nd paragraph</p>'
    );

    $content = get_the_content();

    $output = ''; // define variable to avoid PHP warnings

    $parts = explode("</p>", $content);

    $count = count($parts); // call count() only once, it's faster

    for($i=0; $i<$count; $i++) {
        $output .= $parts[$i] . '</p>' . $additions[$i]; // non-existent additions does not concatenate
    }
    return $output;

}
add_filter('the_content','addParagraphs');

Answer is updated according to subsequent comments:

$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>";
}
7
  • Thanks @Max Yudin, but unfortunately not served to me. I would like to show, for example, after the 1st and the 5th paragraph, not necessarily after the 1st and 2nd.
    – BobGCA2
    Commented Nov 28, 2012 at 20:14
  • @Michael, do you want to use it on per post basis?
    – Max Yudin
    Commented Nov 28, 2012 at 20:24
  • Yes @Max Yudin, I want to display contents after certain posts in the loop, on home page.
    – BobGCA2
    Commented Nov 28, 2012 at 20:29
  • @Michael, does it have to be the same content after one paragraph and the same after other(s)? And do you use it in functions.php or template?
    – Max Yudin
    Commented Nov 28, 2012 at 20:43
  • May be the same content, @Max Yudin. I'm currently using the template.
    – BobGCA2
    Commented Nov 28, 2012 at 20:49
1
add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {

    $ad_code = '<div>Ads code goes here</div>';

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }

    return $content;
}

// Parent Function that makes the magic happen

function prefix_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 );
}
0

I knew it's old question but this answer should help the people still looking for it.

This plugin works perfectly in any theme.

https://wordpress.org/plugins/insert-post-ads

You can choose first / second or whatever you like to display your ads.

0

I was looking for a way to make calls get_template and here I share it in case it is useful

<?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 and acknowledge you have read our privacy policy.

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