How to Add Adsense Ads in Middle of Blog Posts?
Can you display Adsense ads in middle of blog posts? There is no doubt that in-content ads encourage readers to click more, and the higher CTR can earns you more money. While it is easy to add Adsense ads below post titles or after post content, how can you add it in middle of articles?
Of course there are very powerful Adsense plugins that can let you perform custom ad placements almost anywhere you want. But I prefer to add code to functions.php and keep it simple, as much of the features provided by plugins might not be needed.
So how can you add your adsense like this?
Add Adsense in Middle of Post
Here is the code you need to add to the functions.php file of your WordPress theme. [Note it is a good idea to keep backup of functions.php file and any error can make your site unusable and you need to restore the file via FTP]
add_filter( ‘the_content’, ‘qot’ );
function qot( $content ) {
if( !is_singular() )
return $content;
$paragraphAfter = 5;
$content = explode ( “</p>”, $content );
$new_content = ”;
for ( $i = 0; $i < count ( $content ); $i ++ ) {
if ( $i == $paragraphAfter ) {
$new_content .= ‘<div>’;
$new_content .= ‘ADSENSE CODE’;
$new_content .= ‘</div>’;
}
$new_content .= $content[$i] . “</p>”;
}
return $new_content;
}
1. What this code does now is locate the paragraph after which you want to add the code. Right now we add the adlink code after the 5th paragraph. If you want the adds further down the page, you can change the value here from 5 to whatever you need.
$paragraphAfter = 5;
2. It is also important to locate the ads correctly. We have used the middle-ads class here to style the ads. You will typically need to add margins, padding or float elements to correctly position the ads. For example you can use
.middle-ads {margin: 10px 0;}
3. Remember to replace the ADSENSE CODE with the code for your account.
4. If you only want to add the code to single posts, change the is_singular (which will add ads to both posts and pages) to is_single.