fairydragon
fairydragon

Reputation: 146

How to use wordpress custom field variable in javascript map?

Im hoping somebody can help with this. I am displaying a google map on a product page on my wordpress site. I have used Advanced custom fields to get a POSTCODE for the store location. I can output this to the product page with the following:

<?php $postCode = get_field('venu_postcode'); echo $postCode; ?>

But now I can not figure out how to get this value and use it in the Javascript to generate the map based on the postcode(variable). The code below is part of the javascript where the postcode should be.

var address = postCode;

If I type in

var address = 'an actual postcode';

Then the map works perfectly.

I have tried using

var postCode = "<?php Print($postCode); ?>";

to see if I could use the php variable in the javascript but have had no luck.

I keep getting the following error: Geocode was not successful for the following reason: ZERO_RESULTS

Can anybody help?

Upvotes: 0

Views: 430

Answers (1)

WheatBeak
WheatBeak

Reputation: 1031

Print with a capital P isn't a function, you should use echo anyway.

var postCode = "<?php echo $postCode; ?>";

Upvotes: 2

Related Questions