Others

How to insert Google Adsense Ad Codes in Posts?

May some of you want to put your adsense ads or some other advertisements in the posts, not only insert the ads under post title,wrapped the ads on the left or right or under the post content, how about inert your ads anywhere in your post content? Here is a solution: use wordpress “more” tag.

Here is how to insert Google Adsense Ad Codes anywhere in Posts:

  1. Open your wordpress theme you use, find single.php,  then find the php code <?php the_content(”); ?>
  2. Then, use the follwing code insted of <?php the_content(”); ?>
    —————————————————————————————
    <?php $more = 0; the_content(”); ?>
    <div>
    <script type=”text/javascript”><!–
    google_ad_client = “pub-XXXXXXXXXXX”;
    /* 234×60 */
    google_ad_slot = “XXXXXXXX”;
    google_ad_width = 234;
    google_ad_height = 60;
    //–>
    </script>
    <script type=”text/javascript”
    src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
    </script>
    </div>
    <?php after_more_content($post->post_content); ?>
    —————————————————————————————
  3. After you have finish step 2, please open the file: function.php
    Then add these php codes in the function.php file
    —————————————————————————————
    function after_more_content($body) {
    $moretag = ‘<!–more’;
    $content = FALSE;
    $morePos = stripos($body, $moretag);

    if ($morePos !== FALSE || $morePos > -1) {
    $content = substr($body, $morePos + strlen($moretag));
    $morePos = stripos($content, ‘–>’); // reuse variable

    if ($morePos !== FALSE || $morePos > -1)
    $content = substr($content, $morePos + 3); // strip off rest of more tag
    } else {
    // $content = $body;
    }

    $content = apply_filters(‘the_content’, $content);
    $content = str_replace(‘]]>’, ‘]]&gt;’, $content);

    echo $content;
    }
    —————————————————————————————

  4. then, you can add your ads anywhere you want , just click the “more” tag, your ads will appear. Want more ads? Just add more “more” tag.