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 525 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
Post Views Counter Query Loop in Bricks

Post Views Counter Query Loop in Bricks

How we can output the most viewed posts in your Bricks site in a query loop.
Categories:
Pro
Events grouped by months within years based on event date custom field value

Events grouped by months within years based on event date custom field value

How to output events grouped by years and months using three nested query loops in Bricks.
Categories:
Pro
Query Loop in Bricks

Query Loop in Bricks

This Pro tutorial walks you through the steps to use the Query Loop feature in Bricks to display a responsive grid of posts using CSS…
Categories:
Auto Responsive Grids in Bricks

Auto Responsive Grids in Bricks

This tutorial provides the CSS that can be pasted for automatic responsive grids in Bricks Builder. Posts element Each post will get a right padding…
Related Posts by Author in Bricks

Related Posts by Author in Bricks

Querying posts on single posts by the author of the current post.
Categories: