首页 > 解决方案 > 无法从 else 条件中获取字段

问题描述

今晚我遇到了 WordPress ACF 和一些 if / else 条件的问题。这是我的代码:

<?php
    if( get_field('latitude_du_terrain') && get_field('longitude_du_terrain') ) {
        $acf_latitude = get_field('latitude_du_terrain');
        $acf_longitude = get_field('longitude_du_terrain');
        $acf_category = get_field('terrains_categorie');
        $carte = sprintf(
            '[display_map zoom=12 height=600 marker1="%1$s | %2$s | hello world | This is first marker info window message | %3$s"]',
            $acf_latitude,
            $acf_longitude,
            $acf_category
        );
        echo do_shortcode( $carte );
    } else {
        $acf_category = get_field('terrains_categorie');
        $CURRENTPOST_LAT = get_post_meta(get_the_ID(),'_wpgmp_metabox_latitude',true);
        $CURRENTPOST_LON = get_post_meta(get_the_ID(),'_wpgmp_metabox_longitude',true);
        echo $acf_category;
        $carte = sprintf(
            '[display_map zoom=12 height=600 marker1="%1$s | %2$s | hello world | This is first marker info window message | %3$s"]',
            $CURRENTPOST_LAT,
            $CURRENTPOST_LON,
            $acf_category
        );
        echo do_shortcode( $carte );
    }
    ?>

现在我的问题出在代码的 else 部分。

ACF 不显示“terrains_categorie”自定义字段。从这部分代码访问该字段似乎有问题。

我尝试使用另一个 ACF 函数“get_sub_field”,但它也不起作用。

我可能在这里遗漏了一些东西......但我不知道是什么。

在此先感谢您的帮助!

标签: phpwordpressadvanced-custom-fields

解决方案


$acf_category 在 if/else 条件之前不为空吗?首先将其设置为您的条件(因为它在您的测试的两个部分都可用)。


推荐阅读