PDA

View Full Version : Adsense between posts, yep usual question


martyn11post
03-02-2008, 01:17 AM
Where in the code do you put your adsense script and how to prevent it being displayed more than once or twice? Say after first and second post on page.

And what about just above first post?


Cheers,
Martyn

p.s. dont mind a bit of php

SarahG
03-02-2008, 09:08 PM
The code I use in a WordPress template is:

Place this after your get_header()

$postcnt = 0;

Then put the following where you want your ad to be displayed. Change the 3 on the second line to equal how many ads you want displaying (this below will display 3 ads).

<?php
if ($postcnt < 3) { ?>
<p>
<script type="text/javascript"><!--
Google Ad Code Here
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>
<?php
$postcnt++;
} ?>


To put your ads above or below your post depends on your template and markup however the second code block above should be placed between the

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

which starts the WordPress loop, and the corresponding

endwhile;

If you want it above the post and its title then it needs to go above the code <?php the_title() ?> if you want it below the post then place it just before the endwhile; line.

Without seeing your template this is about the best I can suggest, and of course without you mentioning which blog software you use I've just assumed WordPress. If you using something else then you'll need to let us know what and also perhaps show us your template code.

martyn11post
03-04-2008, 11:27 AM
Thanks Sarah, I've tried to follow what you have done.

I am using wordpress, but the ads seem to not display. I managed to put a link unit at the top of the posts area. But nothing shows below posts.

Heres the code, I may have made a silly mistake.
<?php get_header(); ?>
<?php $postcnt = 0; ?>
<div id="top_content"><?php if(function_exists('wp_admin_bar')) wp_admin_bar(); ?></div>

<div id="content">
<div class="seo_fixed_content">
<div id="post_init">

<?php if(have_posts()) : ?>
<P><script type="text/javascript"><!--
google_ad_client = "pub-2853389786034494";
/* 468x15, created 04/03/08 */
google_ad_slot = "1906652837";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></P>
<?php while(have_posts()) : the_post(); ?>

<div class="post_meta" id="post-<?php the_ID(); ?>">
<h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
<div class="date_post"><?php the_time('F jS, Y') ?><?php edit_post_link('Edit', '| ', ''); ?>&nbsp;|&nbsp;<a href="mailto:?Subject=Check This Out&amp;body=I think you'll like this: <?php echo the_permalink(); ?>">email this</a>&nbsp;|&nbsp;<a href="http://www.digg.com/submit" target="_new">digg it</a>&nbsp;|&nbsp;<a href="<?php trackback_url() ?>">trackback</a>&nbsp;|&nbsp;<?php comments_rss_link('comment RSS feed'); ?></div>
<div class="post_bar">
<span class="comment_counter">
<a href="<?php comments_link(); ?>"><?php comments_number('no comment','1 comment','% comments'); ?></a>
</span>
<span class="author">Posted by <?php the_author_posts_link(); ?></span></div>
<div class="post_cat">Published in <?php the_category(', ') ?></div>

<?php if(function_exists("UTW_ShowTagsForCurrentPost")) : ?>
<div class="post_tag">
<?php UTW_ShowTagsForCurrentPost("commalist", array('last'=>' and %taglink%', 'first'=>'Tagged in %taglink%',)) ?>
</div>
<?php else : ?>
<div class="post_tag">
<?php the_tags() ?>
</div>
<?php endif; ?>

<div class="post_content">
<?php the_content("<br />" . "continue reading&nbsp;" . "&quot;" . the_title('', '', false) . "&quot;"); ?>
</div>

</div>
<?php
if ($postcnt < 2) { ?>
<p>
<script type="text/javascript"><!--
Google Ad Code Here
//-->

<script type="text/javascript"><!--
google_ad_client = "pub-2853389786034494";
/* 234x60, created 04/03/08 */
google_ad_slot = "8942153789";
google_ad_width = 234;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

</p>
<?php
$postcnt++;
} ?>

<?php endwhile; ?>
<CENTER><!-- START ADTOLL.COM CODE V1.0 -->
<SCRIPT src="http://adserve.adtoll.com/js/at_ag_10638.js" type="text/javascript"></SCRIPT>
<!-- END ADTOLL.COM CODE V1.0 -->

</CENTER>
<div id="post_navigator"><?php if(function_exists("wp_pagenavi")) { ?><?php wp_pagenavi(); ?><?php } else { ?><?php posts_nav_link() ?><?php } ?></div>


</CENTER>

<?php else : ?>

<h2>The post had been deleted</h2>

<?php endif; ?>

</div>

<?php get_sidebar(); ?>

</div>

<?php mini_sidebar(); ?>

<?php get_footer(); ?>


Any ideas?

Thanks for the help so far Sarah,

SarahG
03-04-2008, 11:44 AM
You've done it right (except you don't need paragraph tags around the script and yes I know I have them I shouldn't either...!). If you view your source code it's printing the correct code out. So if you can't see your ads then either your javascript is incorrect, it's just AdSense not showing any ads (which can happen but there's usually a blank white space there) or you already have too many ads on the page and their automatic limit kicks in.

I can't see that you've exceeded the limit so check on your code. Remove those paragraph tags too, not that it should make a difference as it works for me. My adsense code looks different to that but then again I think they've updated their code output recently so that could be why.

But as far as the PHP is concerned, you've done everything right.

martyn11post
03-04-2008, 04:06 PM
Found my error!:shakefist:

<p>
<script type="text/javascript"><!--
Google Ad Code Here
//-->

<script type="text/javascript"><!--
google_ad_client = "pub-2853389786034494";
/* 234x60, created 04/03/08 */
google_ad_slot = "8942153789";
google_ad_width = 234;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>


One too many <script type="text/javascript">:blush: - removed it and now everthing works! Yeah!

Thanks for all the help Sarah!:bigsmile:

SarahG
03-04-2008, 06:30 PM
Ahh so it was, didn't notice that one earlier. Glad it's working :)