How to split Query Loop with navigation, so element is added after nth post

Hi,
I’m trying to find a solution for the following

Wanted result: page that lists 24 posts of CPT in 3 columns, after every 6th post, should be a horizontal banner using width of 3 columns (turning 3 columns into 1) , but with pagination, so posts 25-48 are on page two, etc… (see sketch)

I thought of two solution paths

1) Multiple query loops on one page for same CPT using offset

3x query loops with a banner html after each one

Problem: this works great on first page, but some posts are repeated on page 2, 3, …
On page 2, posts 7-12 are repeated in the 1st query loop, …

2) Insert banner after nth post in Query Loop

I used solution from How to add elements inside a query loop - #4 by aidardev

Problem: the banner is posted after every 6th, but logically only into first column, not across the width of the 3 columns

Anyone have any idea how I would accomplish this?
Thank you for your help

If this is your only problem with that solution and you’re using CSS grid (which you should for such a layout) you can easily stretch the banners to take up a full row by using some custom CSS on the query loop item:

%root%:nth-child(7n) {
grid-column: 1 / -1;
}

Best,

André

Thanks André, didn’t think of that.
I am using CSS grid 1fr 1fr 1fr
grid-column: 1 / 1; didn’t do anything, but

%root%:nth-child(7n) { grid-column: 1 / span 3;}

worked

I’d avoid using multiple query loops with manual offsets, as pagination can easily cause duplicated or skipped posts. A better approach would be to use a single query loop and insert the banner after every sixth post, then style the banner to span the full grid width using grid-column: 1 / -1. You’ll also need to make sure the pagination is handled by the main query so each page loads the correct 24 posts. This should keep the layout consistent across pages. If you’re using Bricks, you may need a small PHP or CSS customization to get the banner and pagination working together properly.

Oops. The minus got lost. :smiling_face: Glad I could help.

Thanks André. Adding the minus made it work.
My solution of “span 3”, broke the responsiveness of the grid

@misteralex844 how would you insert the banner after every sixth post? I used solution from How to add elements inside a query loop - #4 by aidardev BUT it replaces every 7th post with the banner.

Yes, the pagination creates another problem, the count doesn’t reset on the following pages, so on page two the banner will show on first place instead of 7th

@Nick100, I think the remaining problem is that the banner is being treated as one of the query results, rather than as an extra element between results.

That explains both symptoms: the seventh post gets replaced, and the position becomes awkward when pagination advances.

I’d keep a single Query Loop set to 24 posts per page and leave the actual post query untouched.

Bricks has a bricks/frontend/render_loop filter that lets you modify the HTML produced for each loop item. In principle, you could use it to append a separate banner element after every sixth rendered post.

Something along these lines:

add_filter('bricks/frontend/render_loop', function($output, $element, $container) {
    if (($element['id'] ?? '') !== 'YOUR_LOOP_ELEMENT_ID') {
        return $output;
    }

    static $counts = [];
    $id = $element['id'];
    $counts[$id] = ($counts[$id] ?? 0) + 1;

    if ($counts[$id] % 6 === 0) {
        $output .= '<div class="loop-banner">Banner content</div>';
    }

    return $output;
}, 10, 3);

Then apply grid-column: 1 / -1 to .loop-banner, assuming its parent is your three-column grid.

The important distinction is that the query still returns 24 actual posts. The banners are inserted into the rendered markup afterward, so they don’t consume a post slot or change the pagination offsets.

The counter above is scoped to the loop element and should start fresh on a new page request. I’d still test it with your specific pagination setup, especially if you’re using AJAX pagination, since I haven’t tested this snippet in your project.

Official hook documentation: Filter: bricks/frontend/render_loop | Bricks Academy

Thanks @Mrlaura Does this work for you, I can’t seem to get it to work. Tried to make it work with aid of AI. Then I tried it on a fresh install, but doesn’t work there either.