Custom fields are the user defined information that are assigned to any blog post in Wordpress. They are also called as meta-data. Blog authors usually define the custom fields when they write the blog post to give some extra information like mood, whether,etc. Custom fields are key/value pairs, that means you can define any key and assign any value to this key. To know more about custom fields check out Wordpress.org site. With this explanation, it is easy to control the ad presence in your blog post easily with custom fields.
Control ad presence in blog post with custom fields
As a blog author, you may want to include ad in some of your blog posts and may not want to have ads in other blogs. But it will be tedious to make a code change every time you post a blog. So we are going to define a custom field $show_ad and in the blog template we will check for this custom field. And, if it is YES we will include the advertisement and if it is NO or NULL we are not going to insert the advertisement.
Code changes to check for a custom field
- Open the single.php wordpress template file
- Search for the text
the_contentand after this line, insert the below code. This will put the advertisement in the bottom of every post. If you want to place your ad on top of every post, copy below code above to thethe_contentline. - Code to be inserted
<?php $show_ad=get_post_meta($post->ID, 'show_ad',TRUE); if (strtoupper($show_ad) == "YES") { echo 'YOUR AD CODE IS HERE'; } ?> - Save the changes.
- After this, when ever you want to insert ad in your blog post, just define a custom field
show_adand give it a value “YES”. Your ad code will be inserted to this post alone. If you don’t want to insert ad, just don’t define any custom fields. So simple, isn’t it?
{ 1 trackback }
{ 7 comments }
Hi,
I was just looking for this type of ad insertion in selected posts. Great! Thank you very much for this information.
You are welcome, Venkat!
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru
Very helpful but how would you get the click through stats? Thank you in advance for your help – much appreciated
Hi Rebecca,
I think getting the click through rates (CTR) for the Google adsense ads, is impossible, because Google will never expose these details to outside world, through any APIs.
Hi.Very nice website.I will come back again.
great website and informations.thanx
Comments on this entry are closed.