Home » How-To's

Using wordpress custom fields to control Ads in your blog

23 October 2008 47 views 3 Comments

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

  1. Open the single.php wordpress template file
  2. Search for the text the_content and 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 the the_content line.
  3. 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’;
      }
    ?>
     

  4. Save the changes.
  5. After this, when ever you want to insert ad in your blog post, just define a custom field show_ad and 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 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

3 Comments »

  • Twitterbits » Blog Archive » Search Engine Optimization Study Links said:

    [...] using WordPress custom fields to present advertisements in specific [...]

  • Venkat said:

    Hi,

    I was just looking for this type of ad insertion in selected posts. Great! Thank you very much for this information.

  • Veerasundar (author) said:

    You are welcome, Venkat!

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.