• Resolved Hashsaz

    (@hashsaz)


    Hi,

    Here’s some idea of mine to display a certain message after the last paragraph (i.e. notification or ads etc). I found lot of solutions showed as following:

    <?php
    $paragraphAfter= 1; //shows image after paragraph 1
    $content = apply_filters(‘the_content’, get_the_content());
    $content = explode(“</p>”, $content);
    for ($i = 0; $i <count($content); $i++) {
    if ($i == $paragraphAfter) { ?>
    <div>INSERT HTML STUFF HERE</div>
    <?php
    }
    echo $content[$i] . “</p>”;
    } ?>

    The problem I’m having now is every post of mine will contain words and wordpress owned photos gallery. I would like display the HTML STUFF after last paragraph and before the gallery.

    I knew it’s possible as I have tested the no. of $paragraphAfter, the HTML stuff will not display if the content’s paragraphs are lesser than the no. under $paragraphAfter. Means, the code above just count the <p> but not including (before) the gallery (<div class=”gallery-1″> automatically generated by wordpress script – media.php)

    I believe the solution, should be calculating what’s the no. of last paragraph and appear as the no. for $paragraphAfter. Anyone assist? Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Did you try calculating the total return from your explode?
    $total_p = count($content);
    So I believe the last one is: $last = “$total_p – 1”;

    * ops, I forgot the gallery stuff. Try the above and let me know.

    Thread Starter Hashsaz

    (@hashsaz)

    <?php

    $content = apply_filters(‘the_content’, get_the_content());
    $content = explode(“</p>”, $content);
    $total_p = count($content);
    $last = $total_p – 1; //shows html at last paragraph.

    for ($i = 0; $i <count($content); $i++) {
    if ($i == $last ) { ?>
    <div>INSERT HTML STUFF HERE</div>
    <?php
    }
    echo $content[$i] . “</p>”;
    } ?>

    Yes! Get it done as above. Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add some constant message after the last paragraph’ is closed to new replies.