• Resolved NightWish

    (@nightwish)


    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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.

    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="https://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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Put adsense in the midle of a post.’ is closed to new replies.