Sorry that post may not have made much sense. I have put ‘respect’ as the key for some reason when I meant to put ‘birthday’ so the code will actually look like this:
<div id="bday" style="background-color: red; padding: 20px;">
<?php $custom_field = get_post_meta($post->ID, 'birthday', true);
if($custom_field !== '') {
echo $custom_field;
} ?>
</div>
Try like this…
<?php $custom_field = get_post_meta($post->ID, 'birthday', true); if($custom_field) : ?>
<div id="bday" style="background-color: red; padding: 20px;">
<?php echo $custom_field;?>
</div>
<?php endif;?>
Awesome, that has done the trick! Thanks very much for your help t31os, you were right on the money.
Of course this is the same as doing…
<?php $custom_field = get_post_meta($post->ID, 'birthday', true); if($custom_field) { ?>
<div id="bday" style="background-color: red; padding: 20px;">
<?php echo $custom_field;?>
</div>
<?php } ?>
Depends if you prefer
: and endif;
or
{ and }
Just to be clear, the syntax is valid in both…. I thought the first example just looked easier to read …
Your initial mistake was placing the DIV outside the IF check. Also the if($var !== '') only checks if the field is not equal to '' (or nothing), which is not quite the same as checking if it’s empty/exists…
Glad i could help.. 🙂
Hey thanks for taking the time to explain it all to me t31os, it’s all gonna be really helpful to know for my project.
I think applies to most cases in PHP to…
whie(something) {
}
or
while(something) :
endwhile;
if(something) {
} else {
something else
}
or
if(something) :
else :
something else
endif;
foreach(something) {
}
or
foreach(something) :
endforeach;
Each coder uses a different method, or may combine both… I never quite realised they were the same until a little while back.. which has totally opened up my understanding of PHP…
Most recent enjoyable functions to learn were explode and implode…
I’m still hurting my head with the RegEx stuff (regular expressions), my god is that overwhelming…. once you start mixing in multiple operators [^(?*.) etc…
I think once you start using a few functions other will make sense and so on… then you’ll start to grasp how flexible PHP is… it’s good fun i tell ya!! 🙂
You forgot switch statements… 😉
I know what you mean about regex, though. Have you tried the Regular Expression Library? I’ve picked up some useful snippets from there. Plus there’s an online regexp tester that’s saved me hours of painful testing before now.
Thanks for the link, hadn’t seen that site before, having a read through some bits now… 🙂 Looks dead handy!! 🙂