Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One way to display custom fields using Advanced Custom Fields (ACF) in a dropdown menu that is located outside of the loop is by using the getfieldobject() function of ACF. Here's an example code snippet:

<?php
$field_object = get_field_object('field_name'); // replace 'field_name' with your field's name
$field_values = $field_object['choices']; // store the values of the field in an array

echo '<select>';
foreach ($field_values as $value => $label) {
    echo '<option value="' . $value . '">' . $label . '</option>'; // display each value as an option
}
echo '</select>';
?>

In this code, we first retrieve the field object using the field name. Then, we extract the choices or values of the field and store them in an array. We then loop through the array to display each value as an option inside the select dropdown. Finally, we close the select element. Note that this code snippet assumes that the custom fields are set up as a select dropdown. If the custom fields are set up differently, you may need to modify the code to suit your needs.