Display AdSense ads in WordPress posts

You might be experimenting with AdSense ads on your blog to achieve the best placement, you may also looking to display AdSense in your blog posts.  I’m not talking about wrapping ads around the post body. Lets say, you want to display an AdSense ad unit after every two paragraphs in your post body which you may have seen on many websites also. If not, you may take Hubpages hubs as an example. If you are looking to do so with your WordPress blog, it is possible with plugins as well as without using plugins.

Note: Below given method involves manual editing of your WordPress theme files. Make sure to back up your theme before proceeding.
  1. First, open your theme directory and edit single.php file.
  2. Search for <?php the_content(); ?> (Ctrl + F and search the string) and replace it with the below given code:
    $block = 3; // this indicate that ad will appear after the 3rd paragraph
    $content = apply_filters('the_content', get_the_content());
    $content = explode("</p>", $content);
    for($i = 0; $i < count($content); $i++ ) {
    if ($i == $block) { ?>
    <div style="margin: 10px 0;">PUT YOUR ADSENSE CODE HERE</div>
    <?php }
    echo $content[$i] . "</p>";
    } ?>
    Change $block value according to your requirement. Replace “PUT YOUR ADSENSE CODE HERE” with your AdSense ad unit code.
  3. Save changes. Done! You may also tweak it to adjust according to your blog layout with CSS positioning.
If you are not good at coding, you may use Ad Injection WordPress plugin for the same, which is very easy to use and provides a variety of options to control ads on your WordPress blog.


Post a Comment

Related Posts Plugin for WordPress, Blogger...