WordPress.org

Ready to get started?Download WordPress

Forums

count paragraphs (2 posts)

  1. Joe Greenwood
    Member
    Posted 3 years ago #

    any idea how to count paragraphs of a post so that I could insert ads etc after x paragraphs?

    i know there are plugins already but i prefer to work without plugins

    am looking to add something to functions.php, or else to single.php and page.php

  2. Joe Greenwood
    Member
    Posted 3 years ago #

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

    added this to single.php

    source: http://wordpress.org/support/topic/show-first-and-second-paragraph-on-the-homepage

Topic Closed

This topic has been closed to new replies.

About this Topic