Bricks Query Loop – How to Insert Elements After Every Nth Post

Update: Follow this tutorial instead.


This Pro tutorial provides the steps to output element(s) in a Bricks' query loop after every nth post.

Use case: Show an ad after every 3rd post.

This is based on the previous How to Insert Element(s) Between Query Loop Posts in Bricks tutorial.

It is possible to apply display: grid to the query loop element's parent if a grid layout is needed.

Note

This might break pagination. Implement this only for a non-paginated list of posts.

Step 1

Let's define a couple of custom functions for getting the loop index and the modulus of loop index.

Add the following in child theme's functions.php or a code snippets plugin:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 524 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

How to create filters with IsotopeJS in Bricks (Part 4): AJAX filter and infinite loops, Sorting, List view and Disable animations

How to create filters with IsotopeJS in Bricks (Part 4): AJAX filter and infinite loops, Sorting, List view and Disable animations

This tutorial will explain how to enable the AJAX filter with an infinite scroll query loop container, how to add a sorting function, how to…
Categories:
Displaying SureCart Featured Products & Products Collections with a Query Loop

Displaying SureCart Featured Products & Products Collections with a Query Loop

We walk through how to use a query loop to display SureCart products, featured products & collections.
Categories:
Pro
Dynamic Posts Lightbox in Bricks using GLightbox

Dynamic Posts Lightbox in Bricks using GLightbox

Steps to set create a dynamic lightbox with content from the post inside of the query loop.
Categories:
Pro
Parent Query Loop Object – Traversing Nested Query Loops Up in Bricks

Parent Query Loop Object – Traversing Nested Query Loops Up in Bricks

Bricks enables us to build nested query levels of unlimited levels. Here are a couple of examples: Sometimes, you might want to obtain the parent…
Categories:
Pro
Nested Queries in Bricks – Posts Grouped by Published Years and Categories

Nested Queries in Bricks – Posts Grouped by Published Years and Categories

In the past we showed how posts could be grouped by years in Bricks. This Pro tutorial takes it further by grouping posts by categories…
Categories:
Adding any Custom WP_Query loop to Bricks’ Query Loop

Adding any Custom WP_Query loop to Bricks’ Query Loop

Sometimes we may need to create a custom post loop using Bricks' query loop feature, but wish to use our own WP_Query code for more…
Pro
Bricks Query Loop for Top-level Parent Pages with Children

Bricks Query Loop for Top-level Parent Pages with Children

In the Bricks Facebook group, a user asked: Looking for a custom query to output a list of posts in the same hierarchy that the…
Categories: