Title: homepage ad problem
Last modified: February 2, 2017

---

# homepage ad problem

 *  Resolved [princewap](https://wordpress.org/support/users/princewap/)
 * (@princewap)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/homepage-ad-problem/)
 * hi
    i want to insert ad on post loop in homepage after 1st or 2nd post please
   help me with this i have tried many codes from google but none are working in
   basic theme [http://www.datingmaza.com](http://www.datingmaza.com)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Theme Author [WP Puzzle](https://wordpress.org/support/users/wppuzzle/)
 * (@wppuzzle)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/homepage-ad-problem/#post-8728429)
 * [@princewap](https://wordpress.org/support/users/princewap/), can you show your
   code snippet? I’m try to help you.
 *  Thread Starter [princewap](https://wordpress.org/support/users/princewap/)
 * (@princewap)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/homepage-ad-problem/#post-8732935)
 * i gave got the code from the below article in my previous theme ribosom it was
   working but not working in basic
 * 1. Login to your WordPress administration panel
    2. Select Presentation ->Theme
   Editor from the navigation menu 3. On the Theme Editor page, you will see a list
   of “theme files” to the right of the page. Select the “Main index template” (
   index.php). 4. Find the following line `<?php while (have_posts()) : the_post();?
   >` 5. Above it add the following line `<?php $count = 1; ?>` Comments:-$count
   is the variable and you have to assign a value to that variable. In this case
   the value is 1. 6. Find the line `<?php endwhile; ?>` 7. Add the following code
   directly above it (replace AdSense Code with your code)
 *     ```
       <?php if ($count == 1) : ?>
       AdSense Code 1
       <?php elseif ($count == 2) : ?>
       AdSense Code 2
       <?php endif; $count++; ?>
       ```
   
 * Comment:-We can count how many times the loop has cycled by adding the block 
   of code above. Remember at the start we added <?php $count = 1; ?>, well above
   we check $count, if count is 1 we place adsense code 1, if its 2 we place adsense
   code 2.NOTE : To change which post your Adsense is placed under, simply change
   the count number, for example: $count == 2 will place the Adsense under post 
   2
    8. Find the line `<?php endwhile; ?>` 9. Below it add the following code:
 *     ```
       <?php if ($count != 2 AND $count != 3 ) : ?>
       Adsense Code 3
       <?php endif; ?>
       ```
   
 * Comment:-The adsense will be added after the very last post on the home page 
   because it’s been added after <?php endwhile; ?>It also tells us that if the 
   adove 2 ads are not the last ads than add Google Adsense Code 3 at the end.
    
   10. Click Save, then go view your site to confirm that the Adsense has been added
   to the first post listing on your home page. Note – if you want the same thing
   to show on the category or archive pages then that’s easy. You just need to copy
   and paste what you ‘ve done to either your archive.php or category.php.
 *  Theme Author [WP Puzzle](https://wordpress.org/support/users/wppuzzle/)
 * (@wppuzzle)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/homepage-ad-problem/#post-8743734)
 * Add the following snippet to file functions.php of [child theme](https://wp-puzzle.com/docs/basic/why-use-a-child-theme.html)
   or plugin [FunctionsPHP](https://wp-puzzle.com/functionsphp/):
 *     ```
       function functionsphp_before_loop(){
   
       	if ( is_single() || !is_home() ){
       		return;	
       	}
   
       	global $basic_loop_cnt;
       	$basic_loop_cnt++;
   
       	if ( $basic_loop_cnt == 1 ) {
       		?>
       		<div>this is adv after 1st post</div>
       		<?php
       	}
   
       	if ( $basic_loop_cnt == 2 ) {
       		?>
       		<div>this is adv after 2nd post</div>
       		<?php
       	}
   
       }
       add_action('basic_before_close_post_article','functionsphp_before_loop');
       ```
   
 * Then add your adv codes instead
    `<div>this is adv after 1st post</div>` and `
   <div>this is adv after 2nd post</div>`

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘homepage ad problem’ is closed to new replies.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/basic/1.3.12/screenshot.png)
 * Basic
 * [Support Threads](https://wordpress.org/support/theme/basic/)
 * [Active Topics](https://wordpress.org/support/theme/basic/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/basic/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/basic/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [WP Puzzle](https://wordpress.org/support/users/wppuzzle/)
 * Last activity: [9 years, 5 months ago](https://wordpress.org/support/topic/homepage-ad-problem/#post-8743734)
 * Status: resolved