I have a category page with an if ( have_posts() ) : ?> loop and would like to insert HTML content after the nth post.

I've done this previously with a WP_QUery using an if( $query->current_post == 4 ) statement.

How can I achieve the same effect with a have_posts loop?


1 Answer 1


The global $wp_query is the default query you're working on when you use have_posts(), the_post() etc, so just add

global $wp_query;
if($wp_query->current_post == 4) echo "4!";

global might or might not be necessary (and is certainly only necessary once, so just add it before the while loop), it depends whether you're in the original template or in a template that has been included via a function call.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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