WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

$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.

share|improve this question
up vote 2 down vote accepted

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;


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>";
share|improve this answer
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. – BóbGCA Nov 28 '12 at 20:14
@Michael, do you want to use it on per post basis? – Max Yudin Nov 28 '12 at 20:24
Yes @Max Yudin, I want to display contents after certain posts in the loop, on home page. – BóbGCA Nov 28 '12 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 Nov 28 '12 at 20:43
May be the same content, @Max Yudin. I'm currently using the template. – BóbGCA Nov 28 '12 at 20:49

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

This plugin works perfectly in any theme.


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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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