Insert custom div between posts

You can check your position within the loop using the current_post var of the $wp_query object, and insert markup at a specific spot or at some interval. A simple example:

while( have_posts() ):
    the_post();

    the_title();
    the_excerpt();

    // if current_post is 1, insert the div
    // note that current_post starts at 0,
    // so this will be after the second post's content

    if( 1 == $wp_query->current_post ):
        echo '<div>My div!</div>';
    endif;

endwhile;

Leave a Comment

tech