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 How much does AdSense pay? Get some ideas and benchmark numbers about how much you can earn with AdSense and how much others make with it from this article. The best theme. I started out with Aruba Nights which is a very nice theme as well.. for Blogger., and then decided to do the migration to WordPress and thank you Phil for the amazing A fully responsive (looks great on mobile devices) Theme. Features the option to display in full or summary view with a ‘View Post’ button. 12 Things to Do Before So, you want to apply for to make money from your blog and enjoy the life you always dream of? Who does not anyway? That’s the dream of every blogger to get Tags: things, before, applying, Hey Shouter, Welcome to this exclusive edition of ShoutMeLoud where you will be learning how to create and setup your blog. This guide is exclusively for new bloggers who want to set up their

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.