While there are many posted solutions on how to insert custom components between posts, I haven't been able to find one that addresses pagination. Currently, on my Wordpress archive template, each page shows 12 posts.

While I'm able to insert a custom component after the 2nd post, given the example below, if I also want to add an additional component that is specified after the 13th post, it will never appear since WP starts the count over again. In addition, the same component is returned on every page.

I assume I need to know the total number of pages in advance so there's a number to check against?



$global = $wp_query;
$count = 1;

// get total pages?
$total_pages = $wp_query->max_num_pages;

@if (!have_posts())
  {{ __('Sorry, there are no posts.', 'sage') }}
  {!! get_search_form(false) !!}

@while (have_posts()) @php the_post() @endphp


  @if ($count == 2 )

  @if ($count == 13 )

  @php $count++ @endphp


{!! pagination_bar() !!}
  • 1
    What is this @ syntax in your code?
    – Tom J Nowell
    Nov 3, 2022 at 16:03
  • That's part of Blade's syntax language which is primarily used with Laravel. I'm using the Sage theme roots.io which uses Laravel components and templates
    – scottrod
    Nov 5, 2022 at 17:15

1 Answer 1


I wouldn't do this with a "count" that is reset on the loop, but instead use some of the items in the WP_Query object, like paged value. You can use that to interpret that you are on the second or subsequent pages and then if you are x item on y page (or some other format) you should be solid.

Hope this helps!

Your Answer

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

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