Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
does if($map !="") work? and does the 1st code block show a map and the second fail to do so? –  BandonRandon Feb 24 '13 at 10:18
add comment

3 Answers 3

I would try

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

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

share|improve this answer
add comment

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) {
share|improve this answer
add comment

$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>' : ''; 
share|improve this answer
1  
Please add an explanation to your answer: why could that solve the problem? –  toscho 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. –  toscho Jul 6 '13 at 10:59
    
All right toscho –  Shahinul Islam Jul 6 '13 at 11:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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