Using wordpress custom fields to control Ads in your blog
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?










[...] using WordPress custom fields to present advertisements in specific [...]
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!
Leave your response!
About
Old Articles
Subscribe
Tag Cloud
architect blog browser chrome cinema code css design development dreamz experience festival firefox flash google html iffk iffk-2008 Java javascript jquery kerala modeling movie news password php portal query search sharepoint sql summit Technology theme tip tips Tool tools trend tutorial uml Web web-design wordpressRecent Posts
Most Commented
Most Viewed