Hi,
I'm looking for a way to hide some code if the custom field of that item is empty.
Ex. if "telefoon_concert" is not present it should hide the code:
<span class="label">T:</span> <?php $key="telefoon_concert"; echo get_post_meta($post->ID, $key, true); ?><br />
This is the full code:
<div class="information">
<span class="label">Aanvang:</span> <strong><?php $key="aanvang_concert"; echo get_post_meta($post->ID, $key, true); ?></strong><br />
<span class="label">T:</span> <?php $key="telefoon_concert"; echo get_post_meta($post->ID, $key, true); ?><br />
<span class="label">E:</span> <a href="mailto:<?php $key="email"; echo get_post_meta($post->ID, $key, true); ?>"><?php $key="email"; echo get_post_meta($post->ID, $key, true); ?></a><br />
<span class="label">W:</span> <a href="http://<?php $key="website"; echo get_post_meta($post->ID, $key, true); ?>" target="new"><?php $key="website"; echo get_post_meta($post->ID, $key, true); ?></a>
</div>
This should be applicable for the other custom fields also if they are empty
Sébastien | French WordpressDesigner answers:
<?php
$aanvang_concert = get_post_meta($post->ID, 'aanvang_concert', true);
if($aanvang_concert) { ?>
<span class="label">Aanvang:</span> <?php echo $aanvang_concert; ?><br />
<? } ?>
and same thing for the others
Sébastien | French WordpressDesigner comments:
complete code
<?php
$aanvang_concert = get_post_meta($post->ID, 'aanvang_concert', true);
$telefoon_concert = get_post_meta($post->ID, 'telefoon_concert', true);
$email = get_post_meta($post->ID, 'email', true);
$website = get_post_meta($post->ID, 'website', true);
?>
<div class="information">
<?php
if($aanvang_concert) { ?>
<span class="label">Aanvang:</span> <?php echo $aanvang_concert; ?><br />
<? } ?>
<?php
if($telefoon_concert) { ?>
<span class="label">T:</span> <?php echo $telefoon_concert; ?><br />
<? } ?>
<?php
if($email) { ?>
<span class="label">E:</span> <a href="mailto:<?php echo $email; ?>"><?php $key="email"; ?></a><br />
<? } ?>
<?php
if($website) { ?>
<span class="label">W:</span> <a href="http://<?php echo $website; ?>" target="new"><?php echo $website; ?></a>
<? } ?>
</div>
Filip Van Reeth comments:
works like a charm ;-)
Can you take a look at my other question with ordering by custom field
Pau answers:
try this
<?php if(get_post_meta($post->ID, $key, true) != "") : ?>
<span class="label">T:</span> <?php $key="telefoon_concert"; echo get_post_meta($post->ID, $key, true); ?><br />
<?php endif; ?>
Filip Van Reeth comments:
nopes, does not work ;-)
Pau comments:
did you replace the <strong>$key</strong> to you meta key?
ex:
<?php if(get_post_meta($post->ID, 'telefoon_concert', true) != "") : ?>
<span class="label">T:</span> <?php echo get_post_meta($post->ID, 'telefoon_concert', true); ?><br />
<?php endif; ?>
Cosmin Popovici answers:
<?php if (!empty($key)) { ?>
<span class="label">T:</span> <?php $key="telefoon_concert"; echo get_post_meta($post->ID, $key, true); ?><br />
<?php } ?>
It means if the $key is not empty, then show it.
Baki Goxhaj answers:
Here it goes:
<?php $key="telefoon_concert"; ?>
<?php if( get_post_meta($post->ID, $key, true) != "" ) {
<span class="label">T:</span><?php echo get_post_meta($post->ID, $key, true); ?><br />
<?php } ?>
Baki Goxhaj comments:
Mine solution should work as I have declared the custom field name before checking it is empty or not.
Filip Van Reeth comments:
white page. Does not work
Baki Goxhaj comments:
Did my code produce the white page?
Kailey Lampert answers:
Like this? Let me know if I misunderstand.
<div class="information">
<?php $key="aanvang_concert"; $value = get_post_meta($post->ID, $key, true); if (!empty($value)) { ?>
<span class="label">Aanvang:</span> <?php echo $value; ?><br />
<?php } ?>
<?php $key="telefoon_concert"; $value = get_post_meta($post->ID, $key, true); if (!empty($value)) { ?>
<span class="label">T:</span> <?php echo $value; ?><br />
<?php } ?>
<?php $key="email"; $value = get_post_meta($post->ID, $key, true); if (!empty($value)) { ?>
<span class="label">E:</span> <a href="mailto:<?php echo $value; ?>"><?php echo $value; ?></a><br />
<?php } ?>
<?php $key="website"; $value = get_post_meta($post->ID, $key, true); if (!empty($value)) { ?>
<span class="label">W:</span> <a href="http://<?php echo $value; ?>" target="new"<?php echo $value; ?></a>
<?php } ?>
</div>
Monster Coder answers:
$key="telefoon_concert";
$post_meta = get_post_meta($post->ID, $key, true);
if(!empty($post_meta)){
?>
<span class="label">T:</span> <?php ; echo $post_meta; ?><br />
<?php
}
Utkarsh Kukreti answers:
<div class="information">
<?php if($value = get_post_meta($post->ID, "aanvang_concert", true)): ?>
<span class="label">Aanvang:</span> <?php echo $value; ?>
<br/>
<?php endif; ?>
<?php if($value = get_post_meta($post->ID, "telefoon_concert", true)): ?>
<span class="label">T:</span> <?php echo $value; ?><br/>
<?php endif; ?>
<?php if($value = get_post_meta($post->ID, "email", true)): ?>
<span class="label">E:</span> <a
href="mailto:<?php echo $value; ?>"><?php echo $value; ?></a><br/>
<?php endif; ?>
<?php if($value = get_post_meta($post->ID, "website", true)): ?>
<span class="label">W:</span> <a
href="http://<?php echo $value ?>"
target="new"><?php echo $value ?></a>
<?php endif; ?>
</div>
Dave Holowiski answers:
You'll need to set key first, before you use the if statements. Depending on which side of the div tag you put the php if statement you can have it not even show the div if it's empty.
<?php $key="aanvang_concert"; ?>
<?php if (get_post_meta($post->ID, $key, true)) { ?>
<div class="information">
<span class="label">Aanvang:</span> <?php $key="aanvang_concert"; echo get_post_meta($post->ID, $key, true); ?><br />
<span class="label">T:</span> <?php $key="telefoon_concert"; echo get_post_meta($post->ID, $key, true); ?><br />
<span class="label">E:</span> <a href="mailto:<?php $key="email"; echo get_post_meta($post->ID, $key, true); ?>"><?php $key="email"; echo get_post_meta($post->ID, $key, true); ?></a><br />
<span class="label">W:</span> <a href="http://<?php $key="website"; echo get_post_meta($post->ID, $key, true); ?>" target="new"><?php $key="website"; echo get_post_meta($post->ID, $key, true); ?></a>
</div>
<?php } ?>
John Cotton answers:
Since you're using several custom fields, it would be more efficient to do this:
$custom_fields = get_post_custom($post->ID);
<div class="information">
<span class="label">Aanvang:</span><?php echo $custom_fields['aanvang_concert'][0]; ?><br />
<?php if ( $custom_fields['telefoon_concert'][0] <> '' ) { ?>
<span class="label">T:</span><?php echo $custom_fields['telefoon_concert'][0]; ?><br />
<?php } ?>
<span class="label">E:</span> <a href="mailto:<?php echo $custom_fields['email'][0]; ?>"><?php echo $custom_fields['email'][0]; ?></a><br />
<span class="label">W:</span> <a href="http://<?php echo $custom_fields['website'][0]; ?>" target="new"><?php echo $custom_fields['website'][0]; ?></a>
</div>
..saves all those multiple database calls...
Nilesh shiragave answers:
Here is the code which you want
<div class="information">
<?php
$key="aanvang_concert";
if(get_post_meta($post->ID, $key, true))
{?>
<span class="label">Aanvang:</span>
<?php
echo get_post_meta($post->ID, $key, true);
?><br />
<?php }
$key="telefoon_concert";
if(get_post_meta($post->ID, $key, true))
{
?>
<span class="label">T:</span> <?php echo get_post_meta($post->ID, $key, true); ?><br />
<?php
}
$key="email";
if(get_post_meta($post->ID, $key, true))
{
?>
<span class="label">E:</span> <a href="mailto:<?php echo get_post_meta($post->ID, $key, true); ?>"><?php $key="email"; echo get_post_meta($post->ID, $key, true); ?></a><br />
<?php
}
$key="website";
if(get_post_meta($post->ID, $key, true))
{
?>
<span class="label">W:</span> <a href="http://<?php $key="website"; echo get_post_meta($post->ID, $key, true); ?>" target="new"><?php $key="website"; echo get_post_meta($post->ID, $key, true); ?></a>
<?php
}
?>
</div>