2nd Oct '25
/
0 comments

ACF Flexible Content Gallery Field Bricks PHP Query

In the Bricks Facebook group a user asks:

Does anyone have experience accessing an acf gallery with a php query within a flexible content loop?

Normally I use this to access the gallery as a php query:

$acf_images = get_field("gallery"); if (!empty($acf_images)) { return [ 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'orderby' => 'post__in', 'post__in' => $acf_images, 'posts_per_page' => -1, ]; } else { return []; }

I've also tried to incorporate this:

$acf_row = \Bricks\Query::get_loop_object();

to access the subfield within the loop but I can't seem to target it

This Pro tutorial walks you through setting up a ACF Flexible Content field with a couple of layouts (Text - Images and Images - Text) where the Images part uses Gallery-type fields and outputting the galleries' images using PHP query loops.

The magic sauce is the Bricks' render_tag() function.

Note: No custom code is needed to display the gallery images using a 'Image Gallery' element. This tutorial is for when you want more control on the layout via a query loop instead of using the Image Gallery element.

Step 1

Create a field group for your post type having a Flexible Content field.

Step 2

Edit a Page (or a post of your post type) and populate the field.

Step 3

Let's create a custom function that takes a field name and returns an array Bricks' rendered image IDs in the field value.

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 628 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
ACF Repeater sub field value dynamic data condition in Bricks

ACF Repeater sub field value dynamic data condition in Bricks

This Pro tutorial provides the steps to output an element on single posts (can be of any post type) only if a specified ACF (Pro)…
Pro
Search Results Grouped by Post Types in Bricks

Search Results Grouped by Post Types in Bricks

Updated on 08 Oct 2024 This Pro tutorial provides the steps to arrange the search results by specified post types in a Bricks site. We…
Pro
Limiting the Number of Images in Bricks Image Gallery Element

Limiting the Number of Images in Bricks Image Gallery Element

How we can use Bricks' render_tag() as a simpler way to limit the image count in any Gallery element.
Pro
“My Favorites” in Bricks

“My Favorites” in Bricks

This Pro tutorial provides the steps to set up and use My Favorites in Bricks Builder.
Categories:
How to show data of a post whose ID is the value of a URL parameter in Bricks

How to show data of a post whose ID is the value of a URL parameter in Bricks

In the Bricks Facebook group, a user asks: Hey Everyone,I am trying to create the following:1- I have a Custom Post Type2- I have a…
Categories:
How to List Your Posts Divided by Categories in Bricks

How to List Your Posts Divided by Categories in Bricks

In this tutorial, we'll learn how to display a list of posts divided by each category. The DOM tree Here is how I structured my…
Categories:
Pro
ACF Post Object Query Loop inside a Posts Query Loop in Bricks

ACF Post Object Query Loop inside a Posts Query Loop in Bricks

How to loop through a CPT and within each post, output a list of related posts.
Categories:
Pro
Nav Menus Custom Query Types in Bricks

Nav Menus Custom Query Types in Bricks

Updated on 21 Aug 2024 This Pro tutorial shows how custom query types for each navigation menu can be generated in Bricks. This enables us…
Pro
Recently Viewed Posts Bricks Query Loop

Recently Viewed Posts Bricks Query Loop

How to output the most recent X number of posts (any post type) viewed by the current visitor.
Categories: