0

I am using the following to display google maps using hotel_address custom field.

<iframe width="440" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?q=<?php the_field('hotel_address'); ?>&amp;iwloc=&amp;output=embed"></iframe><br /><small>

And it is working fine, getting hotel address and displaying a map, however I would like to hide this is hotel_address is blank.

I have tried the following

<?php
    $map = get_post_meta($post->ID, 'hotel_address', true);
    if ( $map ) {
        echo '<iframe width="440" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?q="'.$map.'"&amp;iwloc=&amp;output=embed"></iframe><br /><small>';
        echo '';
    }
?>

And can confirm that $map is picking up the address by using echo $map however it just shows a blank box without any map.

Any ideas? Richard

  • does if($map !="") work? and does the 1st code block show a map and the second fail to do so? – Brooke. Feb 24 '13 at 10:18
0

I would try

if ( $map && $map != "" )

as you set the variable, but it can also contain an empty string.

0

To find out the content of a variable you should prefer var_dump() over echo as it gives you the type and the content of a variable.

You could also try to typecast your variable to a boolean value, because an empty string will also evaluate to true in an simple if, but var_dump((bool) "");will give you false.

if( (bool)$map) {
0

$map = get_post_meta($post->ID, 'hotel_address', true);

can return empty value, first need to var_dump($map) to make sure the hotel_address is working

if $map is empty then make sure, $post->ID is working.

Try this

if( !empty( $map )) {  

or try this

$map = get_the_field('hotel_address');
echo (!empty($map)) ? '<iframe width="440" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?q='.$map.'&amp;iwloc=&amp;output=embed"></iframe><br /><small>' : ''; 
  • 1
    Please add an explanation to your answer: why could that solve the problem? – fuxia Jul 6 '13 at 8:44
  • $map = get_post_meta($post->ID, 'hotel_address', true); can return empty value, first need to var_dump($map) to make sure the hotel_address is working, if $map is empty then make sure, $post->ID is working. – Shahinul Islam Jul 6 '13 at 10:49
  • Please update your answer; comments are hidden from our search engine. – fuxia Jul 6 '13 at 10:59
  • All right toscho – Shahinul Islam Jul 6 '13 at 11:04
0

You can use below code. It may work for you

<?php
    $map = get_post_meta($post->ID, 'hotel_address', true);
    if (isset($map) && !empty($map)) {
        echo '<iframe width="440" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?q="'.$map.'"&amp;iwloc=&amp;output=embed"></iframe><br /><small>';
        echo '';
    }
?>
-1

Make attension if this code is not work then do first :

Please add wp_reset_query(); after while loop you taken before this code, and try this

$map = CFS()->get('hotel_address'); 

if($map){

//  show map 

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.