WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Put adsense in the midle of a post. (3 posts)

  1. NightWish
    Member
    Posted 5 years ago #

    Hi,

    How can I put adsense in the middle of the post. I know how to put it in the beginning and at the end of a post by editing single.php but I don't know what code to use to make it appear in the middle.

  2. iamharlan
    Member
    Posted 5 years ago #

    That was a question that I asked in my post about how to place an image at an arbitrary point in the post.

    Someone recommended using a filter....you can find out about those in the codex.

    I am still trying to figure this out as well.

  3. thisismyurl
    Member
    Posted 5 years ago #

    If you load the content into a string using the get_the_content function, you'll be able to insert the AdSense code any where you'd like by determining where you want it and simply pasting the code.

    For example:

    $code = '
    <script type="text/javascript"><!--
    google_ad_client = "pub-9144171931162286";
    /* 300x250, created 8/10/08 */
    google_ad_slot = "0878512762";
    google_ad_width = 300;
    google_ad_height = 250;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    ';
    $text = get_the_content();
    $text = substr_replace($text, "<p>", $code."<p>");
    echo $text;

    This example will insert your AdSense code ($code) into the content of your post ($text) at the beginning of each paragraph by replacing the <p> with $code<p>.

    Depending upon where you'd like the code to actually appear, you'll have to modify the substr_replace command. One way to easily accomplish what you'd like is to include a special tag in the post where you want the ad to appear and then replace it with the substr_replace command.

Topic Closed

This topic has been closed to new replies.

About this Topic