In this post, we will discuss about how to display Adsense within post content in WordPress. When a blogger start carrier then his second choice is to get Adsense to monetize blog after start up of blog. There are many plugin for add Google Adsense code and display within post content but when I have used it got that not so better to display. After use of unusual plugin we have got better to write a snippet to display Adsense within post .Here in this tutorial, you can see given below very simple code which you have to replace with in single.php file.
When we want to keep the Adsense ads within post content on single page then can follow the below given snippet. First of all comment default the_content() function before using below code.
For Single page
<?php $parano = 2; // ads will display 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 == $parano) { ?> <div style="margin: 10px;">PUT YOUR ADSENSE CODE HERE</div> <?php } echo $content[$i] . "</p>"; } ?>
When we want to display Adsense ads within a loop content like on WordPress blog homepage(index.php) then we have to follow some other trick . Just see below given snippet to understand it in detail.
For Homepage
<?php if (have_posts()): $postcount=1; while (have_posts()): the_post(); ?> <?php $postcounter++; if ($postcounter == 3) { ?> <div style="margin: 10px;">PUT YOUR ADSENSE CODE HERE</div> <?php } ?> <?php endwhile; ?>
Here Adsense Ads will display after second post. In this snippet we have separated ads related code with php tag to get better understanding. We can change the $postcounter equal value and adjust the ads places as per require.
In place of given text “PUT YOUR ADSENSE CODE HERE” you have to paste your adsense code within single.php and save it. That is it. Let enjoy ads publishing on your blog. In this way we can customize ads display without use of plugin.