individuelles und professionelles Webdesign, Programmierung und Internetmarketing

Add Adsense between some Posts in WordPress

In WordPress on the frontpage you have normally a flow of posts.

I want to show you how to put any advertising link or Adsense code between one or more of this posts.

We are going to edit the index.php for this, so please make a BACKUP of this file before starting to change anything!!

We will do this by setting a post counter in front of the loop
$postno = 1
inserting the add code inside the loop
if ($postno == 3) do stuff
and make the postcounter count up one digit for every post
$postno++;

In Detail:
Take the index.php of your theme and find:

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

place the counter start between this two lines like this:

<?php if (have_posts()) : ?>
<?php $postno = 1; ?>
<?php while (have_posts()) : the_post(); ?>

as we are going to place to add codes after the first and the third post we find the next:

<?php endwhile; ?>

and insert my suggested code just above this endwhile:

<?php if ($postno == 1) { ?><div align="center" style="margin-bottom: 10px;">
AdSense Code below post No. one replace with your code
</div><?php } ?>
<?php if ($postno == 3) { ?><div align="center" style="margin-bottom: 10px;">
AdSense Code below post No. three replace with your code
</div><?php } ?>
<?php $postno++; ?>

I styled this code to center on the page and give some space below, you are free to remove or change this div style.

FileZilla connected

There are some themes like Arthemia by Michael Jubel which use an other query for posts, so you will not be able to find the if (have_posts()) : part in there.

In a query like in that theme you should look for:

<?php
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=-27,-28&paged=$page&posts_per_page=5"); ?>
<?php $postno = 1; ?>
<?php while (have_posts()) : the_post(); ?>

and place the counter start before while (have_posts()) : the_post(); – I did this here already.

The code to call the add code and count up one by one (as before) is placed in the same way as in default theme before the next endwhile; after the div before, it should look like this:

<?php the_excerpt(); ?>
</div>
</div>
<!-- new code start -->
<?php if ($postno == 1) { ?><div align="center" style="margin-bottom: 10px;">
AdSense Code below post No. one replace with your code
</div><?php } ?>
<?php if ($postno == 3) { ?><div align="center" style="margin-bottom: 10px;">
AdSense Code below post No. three replace with your code
</div><?php } ?>
<?php $postno++; ?>
<!-- new code end -->
<?php endwhile; ?>
<div class="navigation">

happy coding…

Related posts:

  1. Exclude one headline post from frontpage but not if is paged
    The problem: I got a WordPress site with feature headline,...
  2. List some pages with content in Wordpress
    To list pages in WordPress you normally take this: <?php...
  3. List subcategories in category bar of arthemia theme
    In the WordPress theme Arthemia theme by Michael Jubel is...
  4. List one latest post per category on frontpage view
    I will give you here a code to list only...
  5. Google, Adsense, Googlemail und youtube so langsam heute
    Heute ist mal wieder so ein Tag, wo Google nicht...
1. March 2009 • Kategorie: Wordpress Tips Tags: , , ,
These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Facebook
  • LinkedIn
  • TwitThis

JW ist Freelance Webdeveloper

Website Questions? Please ask in Webdesign Forum


You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Kommentieren

Interessante Links zu Adsense between some Posts Wordpress

Revolution Pro Media 3 Released (Wordpress)

Some of the changes that have been included are: All 3 sidebar areas are widget-ready Theme options page inside WP dashboard Comments section coded for Gravatars Google AdSense integration in between posts and comments Archive & Blog page templates

Add adsense between Post Wordpress Plugin Quick adsense

Add Adsense Between Post : Quick Adsense Wordpress plugin. by Harsh ... Though there are various ways by which we can add adsense in between the post. ...

AdSense Between Posts - How to Add Google AdSense Ads Between ...

Three step tutorial to add Google AdSense ads between posts using Wordpress.org.