ProBlogger.net, an extremely popular blogging blog, recently published an article about ad positioning, and how inline ads outperformed other ad positions for him. After this is done, you’ll be able to add inline advertisements to specific posts using custom fields. Not just that, but you’ll be able to specify the ad size as well. Now I know there may be WordPress plugins that are able to do this for you, but this tutorial will go over how to do it manually by editing your WordPress theme.
- A simple diagram of a layout with inline ads in the content
- What a custom field is, what they do, and what their purpose is
- Edit your WordPress theme to accommodate custom fields
- Add a class to your stylesheet to properly display inline ads
- Use a custom field key and value to specify which ad size to use
Maximize your ad revenue by implementing these techniques in your WordPress theme and read on…
Inline Ad Diagram
Please note that I’m not an artist or designer by any means. The following diagram was done in MS Paint. Go ahead and laugh.
If you were unsure of what exactly I meant by an “inline ad” hopefully this clears it up.
Custom Fields
Custom fields can be assigned to any WordPress post or page. You can use them to add additional data to be displayed elsewhere in your theme, within the Loop. The basics of custom fields can be read about on the WordPress Codex.
Edit Your WordPress Theme
Here’s the fun part. Now it’s time to edit your WordPress theme to accommodate the custom field values and keys you’ll be adding to specific posts. Here’s the code I use myself on this blog, minus the actual ad code.
<?php $key="ad_size"; if (get_post_meta($post->ID, $key, true) == "300x250") { ?>
<div class="inlineads">
[Your 300x250 code here]
</div>
<?php } elseif (get_post_meta($post->ID, $key, true) == "250×250") { ?>
<div class="inlineads">
[Your 250x250 code here]
</div>
<?php } elseif (get_post_meta($post->ID, $key, true) == "200×200") { ?>
<div class="inlineads">
[Your 200x200 code here]
</div>
<?php } ?>
Please insert your own 300×250, 250×250, and 200×200 ad codes in the appropriate areas. Feel free to alter this code and use other ad sizes. It’s up to you.
Edit Your Styelsheet
You probably are wondering with the <div class=”inlineads”> and </div> surrounding your ad code is for. This is so we can add a class called “inline ads” to your stylesheet, so you’ll be able to easily manipulate the ad positining by editing just one line in a CSS file.
.inlineads {float:left;padding:0 5px 5px 0;}
This code will float your ad to the left and pad it 5 pixels on the bottom and right sides.
Start Displaying Ads
Here’s the what we’ve all been waiting for - actually displaying the ads. Fortunately all the hard stuff has already passed. In a post, add the custom field key “ad_size” (without quotes) with the ad size (300×250, 250×250, 200×200) you want as the value.
Once you’re done, and everything has been done properly, you should now have an inline ad in your post.
Conclusion
Like I mentioned at the beginning of the guide, there are plugins that may help you do this exact same thing. However this guide wasn’t just about adding ads to your posts, but also to get a better understanding of custom fields and altering WordPress themes. There is literally limitless possibilities when it comes to custom fields, this is just one of the ways you can use them. For another creative way to display ads on your WordPress blog, check out this article I wrote on HackWordPress entitled Display Adsense On Your First Post Within The Loop.
Subscribe to the feed for more Theme Lab tutorials and theme updates. Feel free to comment and share if you liked this tutorial.









Very nice feature explained. Well done Layland!
Great write up Leland!
Leland, you certainly have a knack for creating well thought-out and easy to understand instructions. Even as an experienced WordPress guy, I always find useful explanations that give the complete picture of whatever task your explaining.
Thank you:)
Thanks for the nice comments everyone, greatly appreciate them. Nice to see we’re all using Gravatars as well.
@BANAGO - OOOH.. You spelt Leland’s name wrong..
However, the rest of the tutorial was (:D).
Sorry I know I am a tool. But where in my theme do I add the given code. I am not good with php at all and always screw things up.
Thank you.
Firstly. where is my previous post?
Anyway.
Since I am not very good at PHP I am wondering as to where do I need to place the above PHP code.
Everything else seems easy enough.
Thank You.
@saint_sinner: First-time commenters are are moderated before they go live. Anyway, you would probably want to put this code on your single post template, in single.php. It needs to go in the Loop.
After:
<?php while (have_posts()) : the_post(); ?>And before:
<?php endwhile; ?>Hope this helps.
Ahh. Thanks a lot. and really sorry about the double post.
@saint_sinner: No problem, glad I could help.
I am really sorry of the misspelling - I am a little bit dyslexic in nature
@BANAGO: No worries.
hi, i’m not sure if all that you talk about here will work for me; but i am so excited to really try them. i use wordpress’s free account. do you think anything here will work there?
if not, is there any way, aside from opting for the premium account, i could add excitement on my page in wordpress?
Cool, thanks! I’ll try this out!