Adsense In Wordpress

Status
Not open for further replies.

jerxs

New member
Jun 24, 2006
1,806
24
0
North East PA
So ive been trying to learn a little PHP lately. There are a few plugins for adsense in WP, but still you must place markup in each post you wish the adsense to appear in. The ones ive tryed anyway. Heres a solution.

You can place this in any post or after any post you wish. you can also place multiple ads using different variations. (if you want an ad after the first post and within the content of the first post you can do that by setting up two different variables.

Set up a new .php file and save it under the theme you are using (eg; ads_1.php )

Now in the file create a container <div id="ads-1"> </div>, now place Google, yahoo or whatever ad code inside div and style within your style sheet.

In your index.php file you will want to place this or something similar after the head tag.

<?php $GoogleAds = true; ?>

now find in the index file where you would like to place your ad, say in this case between the first and second posts, after the closing div for the post and post meta data place the following....

<?php If ($GoogleAds) { ?>
<?php include ('ads-1.php'); ?> // or you can just place ad code instead of php file
<?php $GoogleAds = False;} ?>

and there you have it, an ad between your first and second post that will only be displayed between the first an second posts.

Like I mentioned above you can set up another file ads-2.php and set the variable, call the file in your first post, style it and work it right into content. No need to place any ad code in your posts.

I got the basis for this simple code from WP.org, but played around with it a bit. I am going to try a few more things woth it that im sure will work, like placing all php within the ad.php file anf just adding a <? include ('ad1.php'); ?> in the area that id like to place the ad.

I posted this because Ive seen allot of talk about the MMV templates and adding adsense to the first post or after the first post around. This code is your ticket.
 


Here is another way to do it for your main index:

Code:
<?php $count = 0 ?>

place above --> <?php if (have_posts()) : ?>

<?php if ( $count == 0 ) : ?>
<?php include (TEMPLATEPATH.'/ad2.php') ?>
<?php endif // $count == 0?>
<?php if ( $count == 1 ) : ?>
<?php include (TEMPLATEPATH.'/ad3.php') ?>
<?php endif // ( $count == 1 ) ?>
<?php $count++ ?>

place above --> <?php endwhile; ?>

places ads below 1st and 2nd post

:batman:
 
That works if I added

Code:
<?php if ( $count == 0 ) : ?>
<?php include (TEMPLATEPATH.'/ad2.php') ?>
<?php endif // $count == 0?>
<?php if ( $count == 1 ) : ?>
<?php include (TEMPLATEPATH.'/ad3.php') ?>
<?php endif // ( $count == 1 ) ?>
[COLOR=DarkOrange] <?php if ( $count == 10 ) : ?>
<?php include (TEMPLATEPATH.'/ad4.php') ?>
<?php endif // ( $count == 10 ) ?>[/COLOR]
<?php $count++ ?>

It should also add an ad after the 10th or last post on the page if set to 10 posts per page?

Jer
 
Not to come across like a dip shit, but im trying to learn this shit. Even after reading up on operators I am still finding it difficult grasping this, I know == is a comparison operator, and it stands for equal to, but from what I gather from the above;

<?php if ( $count == 0 ) : ?>
<?php include ('ad1.php'); ?>

Why does this ad after the first post? If count is equals zero then ad ad1.php? If you have the time could you posibly explain this to me? Would be much appreciated! Like I said im trying to learn some of this php stuff and im finding some of it a little difficult, nothing like CSS or XHTML thats for sure, lol!

If you know, what would be the proper way to add an ad before the first?

Jer
 
Status
Not open for further replies.