29th Sep '25
/
1 comment

HappyFiles Bricks Query Loop

This tutorial explores different ways in which images from WordPress Media Library organized via HappyFiles can be pulled and shown in Bricks using query loop(s).

Note that these are only needed when you want more control than what the ‘HappyFiles – Gallery’ Bricks element added by HappyFiles provides.

Table of Contents

HappyFiles folder images using nested query loops

Let’s consider the simple case of images in a “Business” folder.

To show these via Bricks query loops, we need to first loop through the ‘Business’ term HappyFiles folder (outer query) and inside that, Media post type attachments in the Business folder (inner query).

Outer query:

Inner query:

Structure:

To make the images clickable to show the full images in lightboxes,

Section JSON (so you copy and paste).

HappyFiles folder images using a single PHP query loop

If you’d like to use a single Bricks query loop, enable PHP query and paste:

// Query attachments in the HappyFiles category
$args = [
  'post_type' => 'attachment',
  'post_status' => 'inherit',
  'posts_per_page' => -1,
  'fields' => 'ids',
  'no_found_rows' => true,
  'tax_query' => [
    [
      'taxonomy' => 'happyfiles_category',
      'field' => 'slug',
      'terms' => 'business',
    ],
  ],
];

$hf_query = new WP_Query( $args );
$image_ids = $hf_query->posts;

// Return query args for Bricks
if ( ! empty( $image_ids ) ) {
  return [
    'post_type' => 'attachment',
    'post_status' => 'inherit',
    'posts_per_page' => -1,
    'no_found_rows' => true,
    'post__in' => $image_ids,
    'orderby' => 'post__in',
  ];
}

// Return empty result set
return [
  'post_type' => 'attachment',
  'post__in' => [ 0 ],
];

Replace business with the slug of your HF folder.

Sign code and save.

Section JSON.

HappyFiles folder images with BricksExtras Dynamic Lightbox

To have full control of what should appear inside the lightbox for each image from a specific HappyFiles folder, use Dynamic Lightbox element in BricksExtras.

For the ‘Small Image’ a CSS class of post-lightbox has been added.

Make sure it’s set to link to #.

Dynamic Lightbox settings:

Section JSON.

For showing the image description, you could use the x_attachment_description dynamic tag enclosed in curly braces.

Other dynamic tags that BricksExtras adds for attachments (images) are:

  • x_attachment_alt_text – Displays the attachment alt text.
  • x_attachment_title – Displays the attachment title.
  • x_attachment_url – Displays the attachment image URL.
  • x_attachment_caption – Displays the attachment caption text.

All the tutorials on HappyFiles in Bricks here. Also, see this Bricks forum post.

Get access to all 613 Bricks code tutorials with BricksLabs Pro

1 comment

Leave a Reply to Rene (Cancel Reply)

 

Related Tutorials..

Pro
Limit the Number of Posts in a Bricks Query Loop of Relationship Type

Limit the Number of Posts in a Bricks Query Loop of Relationship Type

Updated on 12 Dec 2023 Bricks Query Loop popup does not have a control for setting the number of posts to be output when the…
Pro
Child Posts Section in Bricks

Child Posts Section in Bricks

This Pro tutorial provides the steps to limit the posts (can be of any post type) output by Bricks' query loop to children of the…
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:
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:
How to display a Query Loop in 3 columns in Bricks

How to display a Query Loop in 3 columns in Bricks

This tutorial provides the builder settings and CSS codes that can be pasted in order to create a 3-columns query loop container inside Bricks Builder.
Categories:
Pro
ACPT Gallery Field Query Loop in Bricks

ACPT Gallery Field Query Loop in Bricks

How we can output images from ACPT‘s Gallery field for posts as a grid using a query loop.
Categories:
How to create filters with IsotopeJS in Bricks (Part 4): AJAX filter and infinite loops, Sorting, List view and Disable animations

How to create filters with IsotopeJS in Bricks (Part 4): AJAX filter and infinite loops, Sorting, List view and Disable animations

This tutorial will explain how to enable the AJAX filter with an infinite scroll query loop container, how to add a sorting function, how to…
Categories:
Pro
Dynamic HappyFiles Gallery in Bricks with Meta Box or ACF Pro

Dynamic HappyFiles Gallery in Bricks with Meta Box or ACF Pro

Updated on 12 Jul 2023 This Pro tutorial provides the steps to show the images uploaded to an "Image Advanced" type of custom field created…
Categories: