Plugins like ACF, Meta Box and JetEngine enable us to register custom meta for posts in WordPress that are repeating/cloneable. Ex.: A Technology field that has say, Technology Name and Technology Age sub fields.
There could be times when you want an element say, a Bricks Section that contains such repeater fields to be rendered only if there is at least one row in the field i.e., if the field is populated with at least 1 set of sub field values.
We can create a custom function that returns the count of array items and use this with the Dynamic data condition in Bricks to ensure that the Section gets output only if at least one row of data is filled in.
Add this in child theme‘s functions.php
or a code snippets plugin:
function bl_get_repeater_count( $field_name ) {
return count( get_post_meta( get_the_ID(), $field_name ) );
}
and apply a condition like this:
{echo:bl_get_repeater_count(technologies)}
Replace technologies
with the name/ID of your repeating custom field.
7 comments
Eric Embacher
This isn't working for me. No matter how many records in in the repeater, whether zero or 3 records, it always returns 1. Here's an example of my ACF repeater field "videos" that this function returns 1 for: https://www.dropbox.com/s/nmb3x14b3df7gqv/Screenshot%202023-07-23%20at%2010.10.56%20PM.png?dl=0
Eric Embacher
Nevermind...figured it out...
Domenico Mastroianni
Here it is: https://paste.pics/NJWCC
Domenico
Hey Sridhar, thanks for sharing this solution.
I tried to implement it but I am not getting the condition to work.
I have copy/pasted the php code into a snippet.
Set the condition on my div to show it only if my repeater (JetEngine) called 'doorbird' is > 0.
Where did I get lost in this simple tutorial?
Sridhar Katakam
Hey. Possible to provide your WP login so I can take a look?
https://brickslabs.com/support/
Domenico Mastroianni
I didn't received a notification about your reply, just saw this.
I made it work by adding the code into the function.php but it didn't work when added to WPcodebox?
Sridhar Katakam
I've added comment reply notification.
Can you share a screenshot of your WPCB snippet settings?