I have some code that generates a Google Map to show a marker based on the location field supplied via a custom field named ‘event_map’. It works great.
<?php $location = get_field('event_map'); if( !empty($location) ): ?> <div class="acf-map"> <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div> </div> <?php endif; ?>
I now want to add a map that shows ALL of the markers from ALL of the posts. How would I go about doing this assuming all of my posts have a field called ‘event_map’ to draw location data from? I am stumped and any help appreciated.
you need to do something like this:
the key is = to loop first through all post, save marker/values into an array (without echo something), output one single map, and output the markers from saved array
<?php $the_query_map = new WP_Query( array( 'post_type' => 'event', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'menu_order' ) ); if($the_query_map->have_posts()) : while($the_query_map->have_posts()): $the_query_map->the_post(); $the_ID = get_the_ID(); $get_google_map = get_field('event_map', $value); $output_map[$the_ID]['map'] = '<div class="marker" data-lat="'.$get_google_map['lat'].'" data-lng="'.$get_google_map['lng'].'"></div>'; endwhile; endif; wp_reset_postdata(); ?><div class="acf-map"><?php foreach( $output_map as $key => $map_marker ): echo $map_marker['map']; endforeach; ?> </div>
you need to adapt my code that it fit your needs, but i hope with that help you can do it at your own.
It is possible that there has been some change on your site.
Have you done any plugin/theme update?
Kindly try deactivating all the plugins and revert to the default wordpress theme and let see if the code still works. It could be a plugin conflict.
Hope this helps.
I also noticed that this stopped working completely. Unlike Futaba, it still does not seem to be working for me. Was great for months then one day out of nowhere, failed.
No idea why to this day so we had to disable the feature completely unfortunately. I always assumed it was something Google changed on their end as our site and code didn’t change at all.
Quick question for you. How did you manage to get your orignal code to work with out referencing the post ID the only way I can get mine to display a single post is to included the post id
Im using ACF & CPT UI plugins
Cheers for the advice in advance 🙂
<?php $location = get_field('location_now',22); if( !empty($location) ): ?> <div class="acf-map"> <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div> </div> <?php endif; ?>
My aim is to create a stand alone map that loops all the map points but I cant seem to get the template file to work
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2022 Advanced Custom Fields.