I have a custom function that adds one latitude- and one longitude meta box in WordPress.
I’m using the Google Maps API like this in my functions.php
function googlemaps() {
echo "<script>
var map;
function initMap() {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
</script>
";
}
which works fine with static lat and lng values but it I can’t get it to work with my meta values
I did this, which is obviously wrong, but that’s as far as my knowledge takes me
function googlemaps() {
echo "<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: get_post_meta( get_the_ID(), '_lat', lng: get_post_meta( get_the_ID(), '_boyta'},
zoom: 8
});
}
</script>
";
}
You need to get out of your “echo” statement to print the result of
get_post_meta
. Right now, you’re just printing the text. Try this:I’m assuming your lat and long are stored as “_lat” and “_boyta”? The “true” I added tells WordPress that you want the single value, not an array of values.