4th Oct '24
/
0 comments

Bricks Query Loop: Outputting a Fixed Number of Items

Bricks Query Loop: Outputting a Fixed Number of Items

In the BricksLabs Facebook group a user asks:

How might I repeat query loop items until it reaches a specific number?

One service might have 7 images, another might have 10, but there needs to be exactly 13, so they need to loop again until 13 is reached.

This Pro tutorial shows how bricks/query/result filter can be used to intercept the query result and modify it to output a fixed number of items by re-using the items.

We shall cover two types of queries - Meta Box cloneable group and Posts.

Meta Box Cloneable Group

Consider a "Features" group-type field having "Feature Name" and "Feature Details" subfields.

Field populated for a specific Page:

In the Bricks editor setting up a query loop for this Meta Box group shows the three items like this:

The requirement is to show exactly 13 items by filling the remaining 10 slots with the existing ones, like this (screenshot after implementing the tutorial):

Here's how.

Add the following in child theme's functions.php (w/o the opening PHP tag) 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 610 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Meta Box Image Advanced Field Images in Bricks Query Loop

Meta Box Image Advanced Field Images in Bricks Query Loop

Updated on 3 Feb 2025 A user asked: Do you know what the best way to query an image from metabox gallery? I would like…
Categories:
Tags:
Pro
Filtering Bricks Query Loop By Meta Box Relationship

Filtering Bricks Query Loop By Meta Box Relationship

In the Bricks Facebook group a user asks: Consider this setup: CPTs: Service Areas and City Pages Meta Box Relationship: City Pages to Service Areas…
Categories:
Pro
Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

How to register a new dynamic tag for setting excerpt word limits and outputting an ellipsis (...) at the end.
Categories:
Pro
Meta Box Taxonomy Field’s Image Value in Posts

Meta Box Taxonomy Field’s Image Value in Posts

In a project I am currently working on, the requirement is to show the image of an "Image Advanced" type attached to a taxonomy in…
Categories:
Tags:
Pro
Limit the Number of Posts in a Bricks Query Loop of Relationship Type

Limit the Number of Posts in a Bricks Query Loop of Relationship Type

Updated on 12 Dec 2023 Bricks Query Loop popup does not have a control for setting the number of posts to be output when the…