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

Related Tutorials..

Pro
Ordering Posts by Terms in Bricks

Ordering Posts by Terms in Bricks

How to output upcoming events at the top and past events below in a single query loop in Bricks.
Categories:
Pro
Merging ACF Gallery Images with Featured Image in Bricks

Merging ACF Gallery Images with Featured Image in Bricks

Updated on 12 Mar 2025 In the Bricks Facebook group a user asks: Merging images from different fields into a single gallery Hi, I have…
Categories:
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:
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:
Pro
Bricks Slider Images From ACF Flexible Content Sub Field

Bricks Slider Images From ACF Flexible Content Sub Field

Consider the following field group associated with Pages when using ACF Pro: "Page Fields" field group|_"Page Content" Flexible Content field|__"Layout 1" layout|___"Title" Text field|___"Layout 1…
Pro
Term Parent Slug Data Attribute in Bricks

Term Parent Slug Data Attribute in Bricks

How we can set a data attributes on a child taxonomy dynamically based on the parent taxonomy.
Categories:
Pro
Current Post’s Terms in Bricks

Current Post’s Terms in Bricks

Updated on 15 Jan 2024 This Pro tutorial provides the steps to set up a grid of terms (like categories) that the current post is…