My wordpress blog homepage has thumbnail gallery.

I want add adsense image ad code to 5,10 place.

Below code from my index

   <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <div id="post-<?php the_ID(); ?>" <?php post_class('thumb'); ?>>

I found BELOW code from here (wordpress.stackexchange)

  <?php $post_counter=0; ?>
  <?php if (have_posts()) : ?>
  <?php while (have_posts()) : the_post(); ?>
  <?php require('post.php'); ?>
  if ($post_counter == 2 || $post_counter == 3) { ?>
  <?php } ?>
  <?php endwhile; ?>

I have little knowledge in php. I dont know how to modify it. Kindly help me.

Thanks in advance..)

  • Have you seen this solution? – fuxia Feb 2 '13 at 20:16
  • I cant see that where is adsense code ad there? and how to add adsense code in my thumb class? Help me.. – Venkat Feb 2 '13 at 20:25
  • You add the adsense code per widget with that solution. – fuxia Feb 2 '13 at 20:27
  • Thanks for your fast reply. But i want my adsense code in <?php post_class('thumb'); ?>> – Venkat Feb 2 '13 at 20:33
  • I hope that your idea definitely work. But I am newbie for php. So kindly help me. How to register a text widget? Do i add your code end of index.php? – Venkat Feb 2 '13 at 20:45

Here is a slight variation of my other answer.

First we register two new widget areas, sidebars in WordPress-speak.

add_action( 'widgets_init', 'wpse_84250_register_ad_widgets' );

function wpse_84250_register_ad_widgets()
    // used on the first page of main loop only
        array (
            'name'          => 'Ad Widget 1',
            'id'            => 'ad_widget_1',
            'before_widget' => '<div class="frontpage-ads">',
            'after_widget'  => '</div>'
        array (
            'name'          => 'Ad Widget 2',
            'id'            => 'ad_widget_2',
            'before_widget' => '<div class="frontpage-ads">',
            'after_widget'  => '</div>'

You get two new sidebars now in wp-admin/widgets.php. Add text widgets with your ad code here.

enter image description here

Then we insert these widgets into the main loop.

  1. On loop_start we start execution and hook into the_post and loop_end if we are on the front page. You can remove this condition …

    if ( ! is_front_page() )

    … if you want to show the ads on all archive listings.

  2. On each call we increment the internal counter $count by one. When the counter is equal to 6 we are below the 5th post an call dynamic_sidebar() to show the first widget.

  3. When the end of the loop was reached or we are below the 10th post we show the second widget and remove our callback to save execution time.

Loop code:

add_action( 'loop_start', 'wpse_84250_show_ad_widgets' );

function wpse_84250_show_ad_widgets()
    static $count = 0;

    if ( ! is_front_page() )

    if ( 'loop_start' === current_filter() )
        return add_action( 'the_post', __FUNCTION__ )
            && add_action( 'loop_end', __FUNCTION__ );

    $count += 1;

    if ( 6 === $count )
        dynamic_sidebar( 'ad_widget_1' );

    if ( 11 === $count or 'loop_end' === current_filter() ) {
        dynamic_sidebar( 'ad_widget_2' );
        remove_action( 'the_post', __FUNCTION__ );

You can make a plugin from that code, see: Where do I put the code snippets I found here or somewhere else on the web?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.