Home › Forums › Front-end Issues › How to get the data outside the loop? › Reply To: How to get the data outside the loop?
There are several ways that you can do this, the most straight forward is to loop through all of your values outside the loop and gather all of the values into an array, checking to make sure there are not duplicates
$values = array();
// your loop starts
// somewhere inside your loops where you're getting the value
$value = get_sub_field('field_name');
if (!in_array($value, $values)) {
$values[] = $value;
}
// after the end of the loop loop through the
// collectionof $values and echo what is needed
The second method would be to create an acf/update_value filter https://www.advancedcustomfields.com/resources/acf-update_value/. In this filter you can possibly look at the value and store some other post meta value that has the list so you can just get this value instead of doing the loop. This would be pretty complex and would essentially just move the code from the front end into the admin and would probably require additional coding.
Another method would be to use an output buffer http://php.net/manual/en/book.outcontrol.php
ob_start();
// your current loop for outputting content here
// with additions as shown in the first option for gathering values
// after the loop, output the link and font values
// the echo the output buffer wherever it is the content needs to be displayed
echo ob_get_clean();
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.