WordPress.org

Forums

How to Insert Two Ads into Post Content (Without a Plugin) (1 post)

  1. Klark0
    Member
    Posted 3 years ago #

    I am trying to avoid using plugins that say they can do this.

    I'm currently using the code below. It works great, and I'm able to set it to display a single ad unit, left aligned after the second paragraph.

    Some posts on this site run as long as 4,000 words so I'd like to display a second DIFFERENT ad unit, right aligned at the 10th or 12th paragraph.

    How do I modify this code to that?

    <?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 class="alignleft">Ad Code Here</div>
    
        <?php }
        echo $content[$i] . “</p>”;
        } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.