ansarob
ansarob

Reputation: 825

Advanced Custom Fields Radio Button Value not Displaying Until Page Updated

I have a radio button option that shows while editing all pages and posts. It is a simple "Yes" or "No" asking if the user wants the sidebar to display. The field values will be all lowercase "yes" or "no". The problem I'm running into is, the sidebar will not show unless I click edit page and then update page. "Yes" is already selected by default. So essentially, I would have to go into every page and post on the site and click edit then update, which is obviously not ideal. Is my code possibly wrong? I tried setting the default field to "yes" in the custom fields options, but it didn't change anything.

<?php

$display_sidebar = get_field('display_sidebar');

if ($display_sidebar == 'yes') {

?>

    <!-- do something -->

<?php } else { 

    <!-- do something else -->

} ?>

Upvotes: 0

Views: 3113

Answers (1)

Colin Marshall
Colin Marshall

Reputation: 2142

You could check to see if the field is empty and update it to the default setting if it is. Something like this:

$display_sidebar = get_field('display_sidebar');

if (!$display_sidebar) {
    update_field('display_sidebar', 'yes');
}

if ($display_sidebar == 'yes') {

    <!-- do something -->

} else { 

    <!-- do something else -->

}

Upvotes: 1

Related Questions