Home › Forums › General Issues › Get all CF's contained in Slugs Array
Hello, it turns out that I have a very large number of custom Fields grouped by one or more fields of type “Group” because I need to make many specific modifications before showing these fields in the WP Front-End, I have seen myself in need to obtain the different values of the CFS within the ACF Loop. The problem is that using Loops generates a lot of code and I wanted to reduce it just by making different calls (with different arguments) to a function that would generate each part of the content and return its due value.
In conclusion I want to make a function to which just by transmitting at least arguments like:
The function executes the Loop inside it, concatenates the values in the same variable and when finished returns a variable of type String that contains the text resulting from obtaining the values of the CF’s mentioned in the Array (For obvious reasons these CF’s must belong to the last group mentioned in the Groups array).
I’ve tried it in a number of ways, like setting foreach to iterate groups, and CFs, making the function call itself until the final index of the two arrays is reached (Group Slugs and CF’s Slugs); but there is always something that does not work 100%. The last thing I have tried is to iterate the arrays with “Do While” loops, but unfortunately this was worse because now it shows nothing. I suspect that it is a problem in my logic and I have noticed that on other occasions the problem has been that executing the return of the function breaks all the cycles and I don’t know how to solve it.
Below I share my code I hope you can give me a good hand, since I definitely give up I have been stuck for a long time in the same problem.
function lgroupf($agroups, $acfs, $label=false, $theader='', $object=false){
do {
$group = current($agroups);
if ( have_rows($group) ):
while ( have_rows($group) ):
$notlast = next($agroups);
if ($notlast):
the_row();
else:
the_row();
$out1 = $theader?"<b>$theader</b><br>":'';
do {
$ncf = current($acfs);
if (get_sub_field($ncf)):
$vcf = $object?get_sub_field_object($ncf):get_sub_field($ncf);
$nlabel = $label?key($acfs):false;
$out1.= $nlabel?"<b>$nlabel</b>: $vcf<br>":"$vcf<br>";
endif;
return $out1;
$notcf = next($acfs);
} while ($notcf);
endif;
endwhile;
endif;
} while ($notlast);
}
echo lgroupf(['Mediainfo', 'mis-general'], ['ID Codec' => 'misg-id_codec', 'Content Format' => 'misg-formato'], false, 'MEDIA', false);
For this case I have a field of type “Group” with the Slug “Mediainfo”, inside this there is another group with the Slug “mis-general” and within this there are 2 CF contents identified with slugs “misg-id_codec” and “misg-format”.
Thank you in advance for your time.
The topic ‘Get all CF's contained in Slugs Array’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.