jmccole83
3/11/2018 - 12:26 PM

ACF Pro | Use Custom Post Types for Markers

Use the below snippet to add map markers from custom post types.

This example uses a repeater field to allow for multiple markers from one post, use a regular field to for just one marker.

<?php
  $args = array(
  'post_type' => 'cpt-name',
  'posts_per_page' => -1
   );
   $the_query = new WP_Query( $args );
?>

<div class="acf-map">
    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <?php while ( have_rows('countries') ) : the_row(); 

            $location = get_sub_field('country');

        ?>
        <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
            <p><?php the_title(); ?></p>
        </div>
    <?php endwhile; ?>
    <?php endwhile; endif; ?>
</div>