【问题标题】:Trying to get Default Value of ACF Field before post is saved在保存帖子之前尝试获取 ACF 字段的默认值
【发布时间】:2020-04-15 11:15:00
【问题描述】:

我正在尝试获取 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')。此行旨在获取字段组中定义的字段“位置信息”(或至少我认为如此)的默认值。但是,此行返回 false 并将继续返回 false,直到我打开并保存帖子。

最大的问题:有没有办法在不需要先保存帖子的情况下获取默认值?

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    您可以使用acf_get_field() 函数获取所有字段数据。这将返回一个包含 default_value 的数组。

    acf_get_field('location_info')['default_value']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2021-12-08
      • 2023-04-05
      • 2021-08-11
      • 2016-06-29
      • 1970-01-01
      • 2022-11-16
      相关资源
      最近更新 更多