Insert ads within post contents using shortcode

October 16, 2012 // In: Blogging & SEO

There are so many blogs that put ads within contents of post in wordpress. In this tutorial I’ll tell you how you can put adsense or any other ads anywhere in your post using shortcode. You have to make some small changes and put small piece of code in a file and its done. To place ads, while writing post content you just need to write [adsense] where you want to show your ad.

Insert ads within post contents shortcode

Now lets start ..

Step-1

Go to Appearance -> Editor and click on Theme Functions (functions.php) file and add following code before ?>

function create_adsense_shortcode( $atts ) {
return '<div class="inpost_ad">
Your  Ads Code Here
</div>';
}
add_shortcode('adsense', 'create_adsense_shortcode');

Step-2

Replace Your  Adsense Code Here  with your ads codes and click on  update button.

Now you can add ads within your post content just writing [adsense] shortcode any where within post and If you want to stylize your ads then you can add following code to theme stylesheet

.inpost_ad{
float:left;
margin:5px 0 5px 10px;
}

You can aslo add more css to stylize your ads block.

Multiple Sizes of Ads

If you want to add multiple sizes of ads then you write following code in functions.php .

function create_adsense_shortcode( $atts ) {
extract(shortcode_atts(array(
'size' => '1'
), $atts));
switch ($size) {
case '1' :
$ad = '<div class="inpost_ad1">
Your first size Ad Code Here
</div>';
break;
case '2' :
$ad = '<div class="inpost_ad2">
Your second size ad code here
</div>';
break;
}
return $ad;
}
add_shortcode('adsense', 'create_adsense_shortcode');

Replace Your first size Ad Code Here and Your second size ad code here with your ad codes.

Now, You can put different sizes of ads using shortcode [adsense size=’1′] or [adsense size=’2′].

Read Second Method: Insert ads within contents of post in wordpress

Post your comments to tell us whether it works for you?

Insert ads within post contents in wordpress
What is SEO
  • nice article thanks for sharing.