I really wasn’t quite sure what to title this post, but sometimes you need a custom field to have a bit of text before it when the custom field has data in it, but if it doesn’t you don’t want that bit of optional text to appear.
An example would be information about a salesperson or staff member for a variety of reasons. You may want to output the following:
Contact Information
John Doe
Email: john@whatever.com
Office: 999-999-9999
Fax: 999-999-1001
Cell: 555-555-1212
But what if you don’t have a cell number to add and you don’t want “Cell” to appear because not all staff members want to supply a cell number.
<?php
echo "<strong>Contact Information</strong><br /><br />";
echo get_post_meta($post->ID, "agent_name", $single = true);
echo "<br />";
echo "<a href='mailto:";
echo get_post_meta($post->ID, "agent_email", $single = true);
echo "'>";
echo get_post_meta($post->ID, "agent_email", $single = true);
echo "</a><br />";
echo "Phone: ";
echo get_post_meta($post->ID, "agent_phone", $single = true);
echo "<br />";
echo "Fax: ";
echo get_post_meta($post->ID, "agent_fax", $single = true);
echo "<br />";
echo "<br />";
$agentcell = get_post_meta($post->ID, "agent_cell", true);
// check to see if the field has information in it
// and if it doesn't don't echo out "Cell: "
if ($agentcell != "")
{
echo "Cell: ";
echo $agentcell;
echo "<br />";
}
?>