go41

Add Adsense between some Posts in WordPress

Add Adsense between some Posts in WordPress

von Joern am 14. Apr. 2012 | Keine Kommentare

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.

Adsense between some Posts

Adsense between some Posts

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…

Adsense between some Posts Wordpress

Autor:

Du findest mich auch auf Twitter und Facebook!

Schreibe einen Kommentar

Pflichtfelder sind mit * markiert.


Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.