在保存 post 之前尝试获取 ACF 字段的默认值
Trying to get Default Value of ACF Field before post is saved
我正在尝试获取 ACF 字段的默认值,但无济于事。我现在有这个代码:
<section id="location-info">
<?php if( get_field('location_info') && get_field('is_airport') == false && get_field('is_railway_station') == false): ?>
<h3>Location info</h3>
<p><?php the_field('location_info') ?></p>
<?php elseif ( get_field('airport_info') && get_field('is_airport') == true && get_field('is_railway_station') == false): ?>
<h3>Airport info</h3>
<p><?php the_field('airport_info') ?></p>
<?php elseif ( get_field('railway_info') && get_field('is_airport') == false && get_field('is_railway_station') == true): ?>
<h3>Railway info</h3>
<p><?php the_field('railway_info') ?></p>
<?php elseif ( get_field('is_airport') == true && get_field('is_railway_station') == true): ?>
<h3>Airport info</h3>
<p><?php the_field('airport_info') ?></p>
<?php else: ?>
<?php if( get_field('is_airport') == false && get_field('is_railway_station') == false): ?>
<?php echo '1' ?>
<?php get_field_object('location_info') ?>
<?php the_field('location_info') ?>
<?php elseif ( get_field('is_airport') == true && get_field('is_railway_station') == false): ?>
<?php echo '2' ?>
<?php get_field_object('airport_info') ?>
<?php elseif ( get_field('is_airport') == false && get_field('is_railway_station') == true): ?>
<?php echo '3' ?>
<?php get_field_object('railway_info') ?>
<?php else: ?>
<?php echo 'No info for this location' ?>
<?php endif; ?>
<?php endif; ?>
</section>
参见get_field_object('location_info')
。此行旨在获取字段组中定义的字段 'Location Info' 的默认值(或者至少我认为如此)。但是,这一行是 returning false 并将继续 return false 直到我打开并保存 post.
最大的问题:有没有办法在不需要先保存 post 的情况下获取默认值?
您可以使用acf_get_field()
函数获取所有的字段数据。 returns 一个包含 default_value
.
的数组
acf_get_field('location_info')['default_value']
我正在尝试获取 ACF 字段的默认值,但无济于事。我现在有这个代码:
<section id="location-info">
<?php if( get_field('location_info') && get_field('is_airport') == false && get_field('is_railway_station') == false): ?>
<h3>Location info</h3>
<p><?php the_field('location_info') ?></p>
<?php elseif ( get_field('airport_info') && get_field('is_airport') == true && get_field('is_railway_station') == false): ?>
<h3>Airport info</h3>
<p><?php the_field('airport_info') ?></p>
<?php elseif ( get_field('railway_info') && get_field('is_airport') == false && get_field('is_railway_station') == true): ?>
<h3>Railway info</h3>
<p><?php the_field('railway_info') ?></p>
<?php elseif ( get_field('is_airport') == true && get_field('is_railway_station') == true): ?>
<h3>Airport info</h3>
<p><?php the_field('airport_info') ?></p>
<?php else: ?>
<?php if( get_field('is_airport') == false && get_field('is_railway_station') == false): ?>
<?php echo '1' ?>
<?php get_field_object('location_info') ?>
<?php the_field('location_info') ?>
<?php elseif ( get_field('is_airport') == true && get_field('is_railway_station') == false): ?>
<?php echo '2' ?>
<?php get_field_object('airport_info') ?>
<?php elseif ( get_field('is_airport') == false && get_field('is_railway_station') == true): ?>
<?php echo '3' ?>
<?php get_field_object('railway_info') ?>
<?php else: ?>
<?php echo 'No info for this location' ?>
<?php endif; ?>
<?php endif; ?>
</section>
参见get_field_object('location_info')
。此行旨在获取字段组中定义的字段 'Location Info' 的默认值(或者至少我认为如此)。但是,这一行是 returning false 并将继续 return false 直到我打开并保存 post.
最大的问题:有没有办法在不需要先保存 post 的情况下获取默认值?
您可以使用acf_get_field()
函数获取所有的字段数据。 returns 一个包含 default_value
.
acf_get_field('location_info')['default_value']