WordPress Advanced Custom Fields radio button, how can I save the list of available options as an array and also display them on the page?

Viewed 757

I wrote a WordPress custom page template. This uses a radio select button created in the Advanced Custom Fields plugin to allow me to have an 'if' statement placed on the page to render a post based upon the correct selection. This is purely backend, there are no radio buttons in the frontend UX.

The radio button is used so in the backend the client can create custom posts and use this radio select button to assign a country value to each post. It is being used to list retailers of their product by area on their website.

The field title for the radio button is "country" the possible options are;

United Kingdom, Australia, Canada, Europe, Hongkong, Japan, Scandinavia, and USA

The markup for an example loop that displays posts assigned a country value via this radio button looks like this...

<?php 
    $args = array( 'post_type' => 'dealer', 'posts_per_page' => 99, 'order' => 'ASC' );
    $the_query = new WP_Query( $args ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <?php if( get_field('country') == 'Scandinavia' ): ?>
               WordPress post content goes here
    <?php endwhile; wp_reset_postdata(); ?>
<?php else:  ?>
   <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?> 

I need to be able to make this line dynamic <?php if( get_field('country') == 'Scandinavia' ): ?> I need to replace the "Scandinavia" string with a variable. The value of this variable should be set according the list of options (countries) that are set in the custom radio button field, so I presume an array would be the best way to achieve this.

How do I save the options from an Advanced Custom Field Radio button as an array value of a PHP variable?

Is this the best way I can achieve what I am trying to do here? That is, if a new option is added to the radio button in ACF I need to be able to save this into a PHP array. I'd also need to be able to display all the options on the page.

0 Answers
Related