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
Adding active class to the current term/post in a Bricks query loop on Term archives/single posts

Adding active class to the current term/post in a Bricks query loop on Term archives/single posts

Updated on 29 Feb 2024 In the Sibling Terms Bricks Query Loop tutorial, a member asked: How can I set the class "active" to the…
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
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.
How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 1)

How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 1)

This tutorial provides the PHP & JS codes that can be pasted in order to create a dynamic map with markers populated from a custom…
Categories:
Pro
Events grouped by Event Date custom field in Bricks

Events grouped by Event Date custom field in Bricks

This Pro tutorial for Bricks users provides the steps to output posts of an event custom post type dynamically grouped by the value of their…
Categories:
Pro
Previous Two Posts in Bricks Query Loop

Previous Two Posts in Bricks Query Loop

This Pro tutorial shows how we can modify the parameters of a Bricks query to limit the posts to the previous two whilst cycling to…
Pro
Filtering posts by a group field’s subfield value with Meta Box when using a custom table in Bricks

Filtering posts by a group field’s subfield value with Meta Box when using a custom table in Bricks

Filtering a query loop to output only those posts with the value of a Select subfield of a Group field set to Yes when using…
Categories:
Tags: