PHP help (ad units in between wordpress posts 1+2 and 2+3)

Status
Not open for further replies.

wcrmoney

New member
Sep 10, 2007
304
8
0
Hi guys,

I need a little help with some PHP and wordpress. I am trying to put an ad unit in between posts 1+2 and 2+3 on the homepage.

I can manage to put it in between posts 1+2 or 2+3 using the following code:

?php $count = 0 ?
then the if have posts, show posts business.
?php if ( $count == 0 ) : ?
ADSENSE/advertiser code
?php endif // $count == 0?
?php $count++ ?
?php endwhile; else: ?
This will put it in between posts 1+2 and changing

?php if ( $count == 0 ) : ? to
?php if ( $count == 1 ) : ? will make it appear between posts 2+3



But if you try and run both at the same time, they will both appear together (e.g two units between 1+2 or two units between 2+3). Is it possible to get it to appear between 1+2 and 2+3?? Any help would be great.
 


This is how I do it, using the default theme's index page as an example.

Code:
<?php get_header(); ?>

	<div id="content" class="narrowcolumn">

	<?php if (have_posts()) : ?>
<?php $count=0; /* initialize the var outside  the while loop */?>

		<?php while (have_posts()) : the_post(); ?>
<?php $count++; /* putting it here gets your 1 to match your 1st post */ ?>
			<div class="post" id="post-<?php the_ID(); ?>">
				<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
				<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

				<div class="entry">
					<?php the_content('Read the rest of this entry »'); ?>
				</div>

				<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
			</div>
<?php if ( $count < 3 ) {  ?>
/* 
 you said you wanted POST AD POST AD POST right? 
 you could also do "If ( $count == 1 || $count == 2 )" 
  for after the first and the second
 or "if ( $count %2 ==1 ) "
  for after every other one starting with the first.

 insert your adsense code here 

 note that this is outside the post div. You may place it within the post div, 
  depends on your theme.
*/
<?php } /* end if /*?>
		<?php endwhile; ?>

		<div class="navigation">
			<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
			<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
		</div>

	<?php else : ?>

		<h2 class="center">Not Found</h2>
		<p class="center">Sorry, but you are looking for something that isn't here.</p>
		<?php include (TEMPLATEPATH . "/searchform.php"); ?>

	<?php endif; ?>

	</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>
 
If you use the Sandbox framework as the basis of your theme, this is pretty easy, since each post has a unique semantic ID associated with it that's generated when the page is generated. The downside is that it's a Wordpress specific solution.
 
Status
Not open for further replies.