• Skip to main content
  • Skip to primary sidebar

WordPress, Genesis Framework and Storefront customization tutorials

  • Archive
    • Free
    • Premium
  • Blog
  • About
  • Contact
  • Newsletter
  • Membership
  • Login
Home ยป How to add ACF Google Map to frontend

How to add ACF Google Map to frontend

Add single map marker

Add this to functions.php and replace the XXXXX to your Google Maps API key

function add_google_maps_scripts() {	
    wp_enqueue_script( 'google-map', 'https://maps.googleapis.com/maps/api/js?key=XXXXXXXXX', array(), '3', true );
	wp_enqueue_script( 'google-map-init', get_stylesheet_directory_uri() . '/js/acf-google-map.js',  array('google-map', 'jquery'), '0.1', true );
}
add_action( 'wp_enqueue_scripts', 'add_google_maps_scripts' );

//GOOGLE MAPS API KEY - Show the map in the admin
function my_acf_init() {
	
	acf_update_setting('google_api_key', 'XXXXXXXXX');
}

add_action('acf/init', 'my_acf_init');

Add this where you want to display the map

<div class="map">
           
            <?php $terkep = get_field('terkep');

                if( !empty($terkep) ): ?>
                
                <div class="acf-map">
                    <div class="marker" data-lat="<?php echo $terkep['lat']; ?>" data-lng="<?php echo $terkep['lng']; ?>"></div>
                </div>
                
                <?php endif; ?>
        </div>

Add this to style.css

.acf-map {
	width: 100%;
	height: 400px;
	border: #ccc solid 1px;
	margin: 20px 0;
}

/* fixes potential theme css conflict */
.acf-map img {
   max-width: inherit !important;
}

Download the acf-google-maps.js here

Add multiple map marker

<?php 

$args = array(
        'post_type'      => 'xxxxxxx',
        'posts_per_page' => -1,
	);
    
    $the_query = new WP_Query($args);
    
    echo "
        <div class='map-container'>
            <div class='wrap'>
                <div class='acf-map'>";
        while ( $the_query->have_posts() ) : $the_query->the_post();
            $location = get_field('xxxxxxx');
            $title = get_the_title(); 
            if( !empty($location) ) {
        ?>

        	<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
                        <h4><a href="<?php the_permalink(); ?>" rel="bookmark"> <?php the_title(); ?></a></h4>
                        <p class="address"><?php echo $location['address']; ?></p>

        	</div>

<?php
        }
        endwhile; echo '
                </div>
            </div>
        </div>';

wp_reset_postdata();

Filed Under: Free Tagged With: ACF, Map

About Gabor Flamich

I'm a web developer and designer based in Budapest, Hungary. In recent years, I've documented hundreds of solutions I came across during development. This site is an archive for useful code snippets on WordPress, Genesis Framework and WooCommerce. If You have any questions related to WordPress development, get in touch!

Primary Sidebar

Gabor Flamich

Hi! I'm Gabor.
I write tutorials on WordPress and WooCommerce.

MacBook

12 Essential Snippets for Genesis Developers

Subscribe to my Newsletter to view my basic collection of Genesis snippets that I use for my projects!

Sign Up for Free
  • Facebook
  • GitHub
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube
UpdraftPlus Premium

Tags

ACF Ajax Analytics API Bootstrap Breadcrumb category CPT CSS fetch Genesis Google Maps Gutenberg HTML Isotope JavaScript jQuery loop Map Menu Parallax PHP SASS SEO SQL Storefront SVG tab tag manager tags Taxonomy Tool upsell Webpack Wholesale WooCommerce WordPress WPML

Disclosure: Some of the links in this site are affiliate links. I will be paid a commission if you use this link to make a purchase.

  • Privacy Policy / Terms of Service
© 2023 WP Flames - All Right Reserved