functions.php:
Код: Выделить всё
// Enqueue maps.js
function enqueue_maps_js() {
wp_enqueue_script('maps-js', get_stylesheet_directory_uri() . '/js/maps.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'enqueue_maps_js');
// Enqueue Google Maps API
function enqueue_google_maps_api() {
$google_api_key = GOOGLE_MAPS_API_KEY;
wp_enqueue_script('google-maps-api', "https://maps.googleapis.com/maps/api/js?key=$google_api_key&libraries=geometry,marker&callback=initMaps&loading=async", array('maps-js'), null, true);
}
add_action('wp_footer', 'enqueue_google_maps_api');
// Google Map Geocoding Function shortcode able to pin multiple countries/provinces/states on a single map
function display_country_map() {
if (!is_singular('post')) {
return '';
}
global $post;
$terms = wp_get_post_terms($post->ID, 'country');
if (empty($terms)) {
return 'No countries associated with this post.';
}
$country = '';
$region = '';
foreach ($terms as $term) {
if ($term->parent == 0) {
$country = $term->name;
} else {
$region = $term->name;
}
}
// Generate a unique ID for the map container
$unique_id = uniqid('map_');
// Google Map ID
$map_id = 'f4997d02b8b51ae0';
ob_start();
?>
Project Locations
" class="map-container" data-country="" data-region="" data-map-id="" style="height: 500px; width: 100%;">
window.mapContainerId = '';
window.country = '';
window.region = '';
window.mapId = '';
Подробнее здесь: [url]https://stackoverflow.com/questions/78514530/unique-google-map-for-each-wordpress-post[/url]