Home › Forums › Front-end Issues › Google map address
How to render Google map text address only. When I use command below …
I get text address and co-ordinates as follows …
Location: Digiplex- Poway, Poway Road, Poway, CA, United States, 32.954766, -117.03976399999999
Now How to Render as follows ….??? ( No Co-ocrdinates and text as address format)
You could try doing php explode
$address = explode( ',', get_field(‘location’) ); echo $address.'<br />'; echo $address.'<br />'; echo $address.'<br />'; echo $address.'<br />'; echo $address.'<br />';
The above was close… I’m crap with terminology so bear with me. The above doesn’t work because it won’t return the right stuff (see, crap with terminology 😉 )
You have to create a variable for the location field so you can access the address array.
$location = get_field(‘location’)
$address = explode( “,” , $location[‘address’]);
echo $address.'<br/>’; //place name
echo $address.'<br/>’; // street address
echo $address.’,’.$address; // city, state zip
This would display as (using your address example):
I would just hard code the word “Location:” above this if you need it at all.
If you do: print_r(explode( “,” , $location[‘address’]));
You will get the array of information that is available from the address object, in case you want country or don’t want street address, etc. Obviously you can tweak the html to your needs when you echo out each thing.
Hope this was helpful!
@kingafrojoe definitely got me on the right path, so thanks dude!
Hi there, okadots. I have run into the same dilemma: trying to display the address all in one line without the coordinates at the end.
I am new to PHP, so I’m struggling to get your code to work. Can you paste the complete code that you used that worked? I would really appreciate it!
Thanks so much 🙂
sure – here you go. i am using the ACF repeater and the HTML address tag (i’m making a page with multiple contacts listed)
<?php if( have_rows(‘contacts’) ): ?>
<?php while( have_rows(‘contacts’) ): the_row();
$contact_address = get_sub_field(‘contact_address’);
<?php $address = explode( “,” , $contact_address[‘address’]);
echo $address.'<br/>’; //street number and line break
echo $address.’,’.$address; //city, state
<?php endwhile; ?>
<?php endif; ?>
if you do:
echo $address — you would get the name of the place
echo $address — should be the zip code
you can just put in different values to see what each one is, or you can do print_r($address); which will give you the array with all of the values.
Thank you so much! It took a little finagling, but I figured it out. I’m using the Options page add-on, and calling the company address into the footer. Here is the final code that did the trick:
<?php // vars $contact_address = get_field('company_address', 'option'); ?> <address class="footer-address street-address"> <?php $address = explode( "," , $contact_address['address']); echo $address.', '; //street number echo $address.','.$address; //city, state + zip ?> </address>
I really appreciate the help! 🙂
I haven’t always found this to be accurate. If you search by place, the first object in the array is going to be the place name. But, if you search by numbered address, the first object is going to be the address number instead.
Some “places” aren’t going to start with a name if they aren’t registered with Google, for example. Instead, they are going to start with a number address.
This is the solution I ended with:
<?php $location = get_field('google_map'); if( $location ): ?> <?php echo esc_attr($location['street_name']); ?> <?php echo esc_attr($location['street_number']); ?><br /> <?php echo esc_attr($location['post_code']); ?> <?php echo esc_attr($location['city']); ?><br /> <?php echo esc_attr($location['country']); ?> <?php endif; ?>
No state in Belgium but could add
<?php echo esc_attr($location['state']); ?>
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.