7th Jan '23
/
3 comments

Loop Item ID and Loop Item Object in Bricks

If you are working on a Bricks site with specific requirements, chances are you need to grab the ID and/or object of the current item (post/term/user) in the loop when using a Query Loop.

We used the following in several tutorials already on the site but just posting these here as a separate topic for easy reference.

Loop Item ID

\Bricks\Query::get_loop_object_id()

To show it as a dynamic data tag in a text element, register a function:

function bl_get_loop_item_id() {
	return \Bricks\Query::get_loop_object_id();
}

and

{echo:bl_get_loop_item_id}

Loop Item Object

\Bricks\Query::get_loop_object()

To see all the properties of the object:

print( '<pre>' . print_r( \Bricks\Query::get_loop_object(), true ) . '</pre>' );

Reference

https://itchycode.com/bricks-builder-useful-functions-and-tips/

Get access to all 610 Bricks code tutorials with BricksLabs Pro

3 comments

  • Hello, Sridhar,

    Thank you kindly for your code, it has worked perfectly.

    Best regards, Lukas @ webarch.lt

  • I'm calling the following function in a custom query loop to get a post's media index but for some reason it stops returning the object after 80 objects, any idea why?

    function get_media_index(){ $img = \Bricks\Query::get_loop_object(); if($img) return $img->menu_order; }

Leave your comment

 

Related Tutorials..

Pro
Current Single Post Reference From Inside a Bricks Query Loop

Current Single Post Reference From Inside a Bricks Query Loop

Use case: Get the single post’s custom field (like ACF Repeater or Meta Box Group) array row corresponding to the loop’s counter/index
Categories:
Pro
Related Episodes Grouped by Episode Chapters on Single Podcasts in Bricks when using JetEngine

Related Episodes Grouped by Episode Chapters on Single Podcasts in Bricks when using JetEngine

A user asks: How to Display Related Episodes Grouped by Chapters on a Podcast Page in Bricks? Hi everyone, I have two post types created…
Pro
Accordion Grid Query Loop in Bricks

Accordion Grid Query Loop in Bricks

How to set up a accordion in Bricks such that the "titles" are arranged in a grid with the full width content opening below.
Categories:
Pro
CPT Posts Grouped by Custom Taxonomy Terms in Bricks

CPT Posts Grouped by Custom Taxonomy Terms in Bricks

The steps to set up a Service Category terms loop with an inner Service posts loop in Bricks without writing code.
Categories:
Pro
Upcoming Events with ACF Date Sub field Repeaters grouped by Month Years in Bricks

Upcoming Events with ACF Date Sub field Repeaters grouped by Month Years in Bricks

In the comments section of Upcoming Events Accordion in Bricks Grouped by 'Month Year' a member asked: CPT: eventACF Field: event_dates (Repeater) with event_date sub…
Categories:
Pro
Post-specific ACF Repeater Galleries as Sliders in Bricks

Post-specific ACF Repeater Galleries as Sliders in Bricks

How to output ACF Repeater field rows with the images of the Gallery-type sub field as a slider.
Posts Related by Current Post’s Terms in Bricks

Posts Related by Current Post’s Terms in Bricks

Showing other posts assigned to the same categories/tags/custom taxonomy terms as the current post.
Categories: