How to put a banner ad between post 1 and post 2 on homepage only

Register a widget, and call it on the front-page when the the_post action is called second time:

add_action( 'wp_loaded', 'wpse_80202_register_banner_widget' );
function wpse_80202_register_banner_widget()
{
    // used on the first page of main loop only
    register_sidebar(
        array (
            'name'          => 'Banner front-page ',
            'id'            => 'frontpage_banner',
            'before_widget' => '<div class="frontpage-banner">',
            'after_widget'  => '</div>'
        )
    );
}

add_action( 'loop_start', 'wpse_80202_show_banner_widget' );

function wpse_80202_show_banner_widget()
{
    static $count = 0;
    if ( ! is_front_page() )
        return;

    if ( 'loop_start' === current_filter() )
    {
        add_action( 'the_post', __FUNCTION__ );
        return;
    }

    $count += 1;

    if ( 2 === $count )
    {
        dynamic_sidebar( 'frontpage_banner' );
        remove_action( 'the_post', __FUNCTION__ );
    }
}

I would use the Text widget for that:

enter image description here

Another variant of this code here: Add Adsense code in index.php

Leave a Comment