0

I'm trying to display the Google Map object created using the Advanced Custom Field plugin's built-in generator.

I used the stock Javascript/CSS/PHP outlined here as this exactly matches my use case: https://www.advancedcustomfields.com/resources/google-map/

Only problem: The jQuery script seems unable to find the lat/lng html attributes unless the php variable is dumped or echoed in some way.

<div class="map">
    <?php 
        $location = get_field('location');
        var_dump($location); <-- Why is this necessary?
    ?>
        <div class="acf-map">
            <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
        </div>
</div>

My knowledge of PHP is clearly inadequate to understand why this might be the case. The Javascript is being included in an inline script tag within the PHP file itself.

2
  • Your map belongs to a post? page? taxonomy? Commented Jul 13, 2017 at 20:47
  • Page. This code is in the page template file (city.php)
    – S. Coll
    Commented Jul 13, 2017 at 20:53

1 Answer 1

0

Try this code:

<div class="map">
    <?php 
        global $post;
        $location = get_field('location', $post->ID);
    ?>
        <div class="acf-map">
            <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
        </div>
</div>

Sometimes in the template you're loading this you don't have the post that the field belongs to, so you can try to fetch it in the global $post variable.

1
  • Thanks, this helped me arrive at the solution. I just specified the post ID in the get_field call get_field('location', 1441); and it works, no global declaration needed.
    – S. Coll
    Commented Jul 13, 2017 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.