Wordpress Custom Fields Tip

I was working on a Wordpress site where I had in excess of 10 custom fields across the site. Not all of them are used on every post. Also, sometimes even on a specific category not all of the normal ones were being used. For example, an address section of a post that has the following custom fields... Business Name Address Line 1 Address Line 2 City State Zip Phone Fax Website On the custom category template, I call out these custom fields so that the formatting will always be the same. (a lot of site editors like to use gross bright colors that are not in the color palette for the site. And it takes out a guessing game for the editor as they may not remember font-sizes or colors or font weight) So I could use:
<span style="font-size: 16px; color: #FF0000;">
<? echo get_post_meta($post->ID, "Business Name", true); ?></span>
<br />

<span style="font-size: 14px; color: #EFEFEF;">
<? echo get_post_meta($post->ID, "Address Line 1", true); ?></span>
<br />

<span style="font-size: 14px; color: #EFEFEF;">
<? echo get_post_meta($post->ID, "Address Line 2", true); ?></span>
<br />

<span style="font-size: 14px; color: #EFEFEF;">
<? echo get_post_meta($post->ID, "City State Zip", true); ?></span>
<br />

<span style="font-size: 12px; color: #000000;">
<? echo get_post_meta($post->ID, "Phone", true); ?></span>
<br />

<span style="font-size: 12px; color: #000000;">
<? echo get_post_meta($post->ID, "Fax", true); ?></span>
<br />

<span style="font-size: 12px; color: #000000;">
<? echo get_post_meta($post->ID, "Website", true); ?></span>
<br />
The problem with this is that I have chosen to print all of them with line breaks at the end of them. So if the editor choses not to use one of these for whatever reason...like there is not Address Line 2 for the company, there will be an unwanted line break on the screen. The way to avoid this is to use the following code, which checks first to see if the field is blank first and then if it is blank then it does not print the field or the line break associated with it.
$value = get_post_custom_values("Address Line 2");
        if ($value!="") {
                echo $value[0] . "<br>";
        }
Post a comment
  1. Miklas Njor

    I think you could also wrap them inside a div or li and style those accordingly






Real Time Web Analytics ^