Support

Account

Home Forums Backend Issues (wp-admin) Display markers of a GM Map based on the selection of a taxonomy and gm field

Solved

Display markers of a GM Map based on the selection of a taxonomy and gm field

  • I have a custom post type locations containing a Google Maps field location-gmap and a few text fields for street, zip code, city and possible url.

    Attached to the locations custom post type i’ve attached a custom taxonomy thelocations(with the custom post type ui plugin) which is hirarchical. I have created six locations, 2 locations have the term group1 3 locations have the term group2 and one location has the term group3.

    Then i’ve created an ACF options page containing a taxonomy field op-contact-tax. The taxonomy is the thelocations with the field type Multi Select.

    Displaying ALL locations as markers in the map works without a problem based on this article: http://www.advancedcustomfields.com/resources/field-types/google-map/

    But when i try to narrow the selection down with the aid of the taxonomy field and show only the location of the terms group1 and group3 a new code snippet is necessary; based on the following article http://www.advancedcustomfields.com/resources/field-types/taxonomy/ (the basic display of multiple values). The code i use is the following:

    <div class="acf-map">
    <?php $terms = get_field('op-contact-tax', 'option');
      if( $terms ): foreach($terms as $term): 
      $location = get_field( 'location_gmap', $term ); ?>
      <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
        <h5><?php the_title(); ?></h5>
        <p class="address"><span><?php echo the_field( 'location_street', $term ); ?></span><span><?php echo the_field( 'location_postalcode', $term ); ?></span> <span><?php echo the_field( 'location_city', $term ); ?></span></p>
        <?php $locationurl = the_field('location_url', $term);
        if(!empty($locationurl)) { ?>
           <a><?php echo $locationurl; ?></a> 
           <?php }
           endforeach; ?>
      </div>
      <?php endif; ?>
    </div>

    Problem is no markers are shown at all nor print_r() gives any output at all (no matter if i try to display $terms or $location). Has anyone an idea how to manage that goal? :/ Best regards Ralf

  • Ok i got it working somehow but i have only two follow up questions regarding the initial problem and the new code. The working snippet looks like that:

    <div class="acf-map"><?php
        $terms = get_field('op-contact-tax', 'option');
        $args = array(
          'post_type' => 'locations',
          'posts_per_page' => -1,
        );
    	$the_query = new WP_Query( $args );
    	if( $the_query -> have_posts() ) : while ( $the_query -> have_posts() ) : $the_query->the_post();
            $termstd = get_the_terms($post->ID, 'thelocations');
            $termsingle = array_shift( $termstd );
            foreach($terms as $term):
            	if( $termsingle->term_id == $term ) {
            	$location = get_field( 'location_gmap' );
            	$termdesc = term_description($term, 'thelocations'); ?>
            	<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>" data-col="<?php echo $termdesc; ?>">
                	<h5><?php the_title(); ?></h5>
                	<p class="address"><span><?php echo the_field( 'location_street' ); ?></span><span><?php echo the_field( 'location_postalcode' ); ?></span> <span><?php echo the_field( 'location_city' ); ?></span></p>
                	<?php $locurl = the_field( 'location_url' );
                	if(!empty($locurl)) { ?>
    		    		<a><?php echo $locurl; ?></a>
    		    	<?php } ?>
                </div>
          	<?php }
        	endforeach;
        endwhile;
    		wp_reset_postdata();
        else: ?>
            <p>Error</p><?php
    	endif; ?>
    </div>

    The questions:
    1) Would it be possible to move the closing div at the end, a bit upwards, between the endforeach and endwhile so that it would be possible to query within the same loop for more informations which don’t go into the google maps map. I mean you have the loop, within the loop you query for the informations of the google maps block and then also query for blocks of addresses underneath the google maps part and then close the loop. Or do i have to create two separate loops (one for google maps one for addresses). cuz if i move the div up right after the endforeach there is no map displayed anymore.

    2) Is it possible to solve the issue in a more elegant way. For me the code looks a bit messy.

    Best regards Ralf

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Display markers of a GM Map based on the selection of a taxonomy and gm field’ is closed to new replies.