Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
$20
hide if custom field is empty
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> <?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>
This should be applicable for the other custom fields also if they are empty
This question has been answered.
Filip Van Reeth | 01/14/11 at 9:55am
Edit
(10) Possible Answers Submitted...
See a chronological view of answers?
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
-

Last edited:
01/14/11
10:03amakosipau says: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; ?>Previous versions of this answer: 01/14/11 at 10:02am | 01/14/11 at 10:03am
- 01/14/11 10:07am
Filip Van Reeth says:nopes, does not work ;-)
- 01/14/11 10:12am
akosipau says:did you replace the $key 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; ?>
- 01/14/11 10:07am
-

Last edited:
01/14/11
10:09amSébastien | French WordpressDesigner says:<?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- 01/14/11 10:07am
Sébastien | French WordpressDesigner says: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> - 01/14/11 10:09am
Filip Van Reeth says:works like a charm ;-)
Can you take a look at my other question with ordering by custom field
- 01/14/11 10:07am
-

Last edited:
01/14/11
10:04amCosmin Popovici says:<?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.Previous versions of this answer: 01/14/11 at 10:04am
-

Last edited:
01/14/11
10:03amBaki Goxhaj says: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 } ?>- 01/14/11 10:05am
Baki Goxhaj says:Mine solution should work as I have declared the custom field name before checking it is empty or not.
- 01/14/11 10:06am
Filip Van Reeth says:white page. Does not work
- 01/14/11 10:08am
Baki Goxhaj says:Did my code produce the white page?
- 01/14/11 10:05am
-
Last edited:
01/14/11
10:03amKailey Lampert says: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>
-

Last edited:
01/14/11
10:04amMonster Coder says:$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
} -

Last edited:
01/14/11
10:04amUtkarsh Kukreti says:<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>
-

Last edited:
01/14/11
10:09amDave Holowiski says: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 } ?>
-

Last edited:
01/14/11
10:09amJohn Cotton says: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... -

Last edited:
01/14/11
10:10amNilesh shiragave says: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>
This question has expired.
Current status of this question: Completed
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
