Show content after the first and second paragraph

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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 );
}

Method 3

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.

Method 4

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x