Support

Account

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();