Saturday, January 26, 2008

How to place an ad below first post only in WordPress

Google limits the number of ads that you can place in your blog per page. When you place the adsense code by modifying index.php so that the ad appears below a post, the ad is going to appear beneath all the posts on your blog’s homepage. This is not what we want.

For the ad to only appear under the first post and nowhere else,

The Quick Way

Edit index.php and place the following code wherever you want the ad to appear:

  1. if ($wp_query->current_post <>
  2. adsense_deluxe_ads('above_posts');
  3. } ?>

Replace adsense_deluxe_ads(’above_posts’); with your adsense code. If you want the ad to appear below the post, you would paste the above before

Ad appearing below first post only

Source.

We’ve similarly placed 3 link units at the top of first three posts on our home page using this code:


  1. f ($wp_query->current_post <>
  2. {
  3. include('adsense_indexpage_firstthreeposts_linkunit.php');
  4. }
  5. gt;

3 Link Units in first three posts

The Long Way (We are no longer using this method, now that we’ve discovered the quick way)

  1. Open your theme’s index.php, and find this line:

    1. if (have_posts()) : while (have_posts()) : the_post(); ?>
  2. Immediately above that, paste this:


    1. $postnum = 1;
    2. $showadsense1 = 1;
    3. ?>
  3. Staying in index.php, scroll down to this part:
    1. class="postmetadata">Posted in ', ') ?> | 'Edit', '', ' | '); ?> 'No Comments »', '1 Comment »', '% Comments »'); ?>


  4. endwhile; ?>
  • Above , paste this:
    1. if ($postnum == $showadsense1) {
    2. echo '
    3. Your adsense code goes here
    4. ';
    5. } ?>

    6. $postnum++; ?>

    Paste the adsense code where it says Your adsense code goes here.
    If you are using Adsense-Deluxe plugin, you can replace

    1. echo '
    2. Your adsense code goes here
    3. '

    with the plugin provided code like

    1. 'adsense_below_first_post'); ?>

    This can similarly be extended to archives and search page by editing archive.php and search.php respectively.

    Source.

    No comments: