0

I'm facing a problem tonight with WordPress ACF and some if / else conditions. Here is my code :

<?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 );
    }
    ?>

Right now my problem is in the else portion of the code.

ACF is not displaying the "terrains_categorie" custom field. It seems that there is a problem accessing this field from this part of the code.

I tried with an another ACF function, "get_sub_field", but it's not working too.

I'm probably missing something here... but I don't know what.

Thanks in advance for your help!

Melvins G.
  • 23
  • 4

1 Answers1

0

Is $acf_category not empty before the if/else condition? Start by setting it out of your conditions (as it is available on both parts of your test).

clobee
  • 21
  • 5
  • Yes I already tried that, in this way, it is still available in the IF, but not in the ELSE. Thanks. – Melvins G. Dec 14 '18 at 23:55
  • It's as if ACF is able to go well find some fields in the IF but not in the ELSE. Strange. Thanks again. – Melvins G. Dec 14 '18 at 23:58
  • @MelvinsG. I tried your code and with ur fields and it is working fine for me.I think u r doing some mistake with field slug or may u had not stored its value(It might be empty). – Krishna thakor Dec 15 '18 at 05:37
  • Hi @MelvinsG. if ACF is available in IF and not in the ELSE then your field ACF might be tight to "get_field('latitude_du_terrain') or/and get_field('longitude_du_terrain') ". The answer would probably be on how these two fields affect the ACF, what happens in your code before the condition you've posted? AS a wild guess this https://stackoverflow.com/questions/30578415/php-if-else-get-field-with-wordpress?rq=1 will probably help you – clobee Dec 15 '18 at 12:31
  • Hey @clobee, unfortunately the thread you linked didn't help me, but thanks a lot.The problem seems to be that the $acf_category is not retrieve only inside the else condition weither if I call it inside it, beside it or whatever I tried to define it again. Strange. – Melvins G. Dec 15 '18 at 18:41
  • @Krishnathakor : your anwer helped me a lot! I just did another tests and I think my code is wrong, $acf_category is only print if I fill 'latitude_du_terrain' and 'longitude_du_terrain'. So I should modify my else in order to make him check if the values latitude and longitude are empty :) checking this now. Thanks again. – Melvins G. Dec 15 '18 at 18:52
  • Ok correcting myself, just a bug with my WordPress and ACF, when I simply re-submit my article in WP everything went well, it was something stuck in the pipes apparently. When I create new articles everything goes well. Thanks everybody for your lights :) – Melvins G. Dec 15 '18 at 18:57