0

I have a problem to hide a div when the input of a custom field is empty. This is the code I am using, it's fine when the field is not empty. But when the fireld empty, the div will show.

The field that displays the content is postintro2, the div I want to hide when empty is

<div class="single2-intro">

The complete codes is:

<?php { $postintro2 = get_post_meta ($post->ID, 'postintro2', $single = true);
if($postintro2 !== '') 
echo '<div class="single2-intro">'.$postintro2.'</div>';                            
} ?>

Thanks for any helps.

1 Answer 1

0

Try:

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

if ( ! empty( $postintro2 ) ) {
     echo '<div class="single2-intro">'.$postintro2.'</div>';                            
}

From the codex: get_post_meta

3
  • Unfortunately, the div still appear when the field is empty. Commented Feb 6, 2021 at 6:22
  • It may look empty, but not be. Try var_dump( $postintro2 ); before the if statement.
    – shanebp
    Commented Feb 6, 2021 at 21:00
  • Thank you. It's work. Commented Feb 7, 2021 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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