Home › Forums › General Issues › Default values by option page on first load › Reply To: Default values by option page on first load
@hube2 thanks for helping:
I have find a way to solve it:
First I set the fields as globals with there default values in function.php.
function acf_global_vars() {
global $white_menu;
$white_menu = get_field('top_nav_color', 'option');
if ($white_menu === NULL) { $white_menu = '1'; } // default value
// ------ //
global $image_white;
$image_white = get_field('logo_white', 'option');
if ($image_white === NULL) {
$image_white['url'] = get_template_directory_uri() . '/img/small-business-logo-white.svg';
$image_white['alt'] = 'hallo';
} // default value
// ------ //
global $image_normal;
$image_normal = get_field('logo_normal', 'option');
if ($image_normal === NULL) {
$image_normal['url'] = get_template_directory_uri() . '/img/small-business-logo.svg';
$image_normal['alt'] = 'hallo';
} // default value
// ------ //
global $logo_white_width;
$logo_white_width = get_field('logo_white_width', 'option');
if ($logo_white_width === NULL) { $logo_white_width = '200'; } // default value
// ------ //
global $logo_normal_width;
$logo_normal_width = get_field('logo_normal_width', 'option');
if ($logo_normal_width === NULL) { $logo_normal_width = '200'; } // default value
// ------ //
global $top_search;
$top_search = get_field('top_search_show', 'option');
if ($top_search === NULL) { $top_search = '1'; } // default value
}
add_action( 'template_redirect', 'acf_global_vars' );
Than I call the globals in the template header.php
global $white_menu, $image_white, $image_normal, $logo_white_width, $logo_normal_width, $top_search;
And start the IF
<?php if( $white_menu ): ?>
<img class="logo-white" src="<?php echo $image_white['url']; ?>" alt="<?php echo $image_white['alt']; ?>" width="<?php echo $logo_white_width; ?>" />
<?php endif; ?>
Is this the best method I can have?
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!
ACF PRO’s Flexible Content field allows you to create smaller pieces of UI and compose them into whole flexible patterns. In our latest article, we show how to use it to create swappable site sections and integrate it all in a theme.https://t.co/ZRocH8oJSp
— Advanced Custom Fields (@wp_acf) January 24, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.