Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Display Ad Blocks in Specific Posts in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Display Ad Blocks in Specific Posts in WordPress

As the title goes, this tutorial will pretty much try to cover (at a beginner level) all the steps that need to be taken in order to have the power of choosing in which articles and where exactly to post your ad blocks. An ad block can be AdSense, AdBrite, basically it doesn’t matter if it’s a link to an image or a JavaScript piece of code. The advantage of doing this is that you can always prepare something new for your readers. You won’t always get to have that 468×60 under your post because having the same positions for the ads will make them invisible for your visitors. So I will show you how to get things in control and put those ads where you want, and when you want them.

Display Ad Blocks in Specific Posts

The first thing you’ll need to do is to open your functions.php file of your theme with a basic text editor (Notepad will do). If you don’t have one, just create the file now (it should be located in your theme’s folder). Add the following piece of code to the end of the file (in this tutorial I will be editing the functions.php file of the default theme):

<!– here starts our custom code –>
function adsense_shortcode( $atts ) {
extract(shortcode_atts(array(
‘format’ => ‘1’,
), $atts));

switch ($format) {
case 1 :
$ad = ‘<script type=”text/javascript”><!–
google_ad_client = “pub-xxxxxxxxxxxxxxx”;
google_ad_slot = “xxxxxxxxx”;
google_ad_width = 468;
google_ad_height = 60;
//–>
</script>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>’;
break;
}
return $ad;
}
add_shortcode(‘adsense’, ‘adsense_shortcode’);
<!– the custom code just ended here –>

All you need to do next to output the ad block in your post is to use [adsense] syntax. Notice the $ad variable. In this example it holds the basic AdSense code. Feel free to replace that code with any ad code. As I said earlier, you can have something as basic as a simple image that links to some site (with referral code or such). It is important to keep in mind the size of the code that you’re using. You probably don’t want to end up putting a wide skyscraper in your article; but then again… who knows. If you want to have different ads just duplicate the code above and instead of the following lines have adsense replaced with the new name:

function adsense_shortcode( $atts ) {

replace with

function newName_shortcode( $atts ) {

and

add_shortcode(‘adsense’, ‘adsense_shortcode’);

replace with

add_shortcode(‘newName’, ‘newName_shortcode’);

That should take care of having multiple ad variants. Don’t forget that now you need to call the new ad block by using [newName] instead of [adsense]. Of course there are other more complex methods of doing this but the fact is that I wanted to write a very very easy to follow tutorial.

Hope this was useful for at least someone out there. If you don’t want to dirty your hands with code then you should probably use the WhyDoWork AdSense Plugin.

DanDan Vaida is a 20 years old system administrator and web developer from Oradea, Romania. He is passionate about his work, and he also loves spending time with friends in the nature. You can check out his blog if you’re looking to make new friends, or if you simply need help with something that’s related to computers. You can also follow him on twitter as well.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Usman says:

    Hello,

    Thanks for the article. I’m using quick adsense for display and hide block ads but I need another typre of plugin for my website and sure that wpbeginner team resolve my query.

    I have a blog where I have different type of posts.

    Now my focus on those post which have more than 100 words… is there is any plugin available who give access of this type query, like: any post more than 100 words so ads should be visible and any post have less than 100 words so ads will be hide..

    Hope you understand my query.

    Thanks.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.