[wp-hackers] How you handle empty metadata

Diana K. C dianakac at gmail.com
Fri Aug 31 16:23:52 UTC 2012


Great! I will try out this, thanks.


----- Original Message ----- 
From: "Jackson Whelan" <jw at jacksonwhelan.com>
To: <wp-hackers at lists.automattic.com>
Sent: Friday, August 31, 2012 7:40 AM
Subject: Re: [wp-hackers] How you handle empty metadata


> On 8/30/12 5:22 PM, Diana K. C wrote:
>> I created a filter that places content on posts and pages but since I'm 
>> combining some tags such <li> etc I endeup with silly empty tags if any 
>> value is provided  :(
>>
>> One or two ok check with (!empty)  but I have ten values, what is the 
>> best pratice?!
> I won't claim this is 'best practice', but... I use a handy little 
> function to check if a post meta value is set before echoing.
>
>    // In my functions.php file
>
>    function echo_if_meta( $arr, $key, $before = '', $after = '' ) {
>
>         if( $text = $arr[$key][0] )
>             echo $before . $text . $after;
>
>    }
>
>    // In my template
>
>    <?php $meta = get_post_custom(); ?>
>    <ul>
>         <?php echo_if_meta( $meta, '_my_key', '<li>My Key: ', '</li>'); ?>
>         <?php echo_if_meta( $meta, '_my_key_2', '<li>My Key 2: ',
>    '</li>'); ?>
>    </ul>
>
> You could adapt this for a returned value in a filter.
>
> In short, write yourself a little function to check if the value is set 
> and return null if not, and return the value wrapped in start and end tags 
> if set.
>
> Hope that helps,
>
> Jackson
> -- 
>
> *Jackson Whelan* / WordPress Web Design & Development
>
> Pittsford, Vermont / 802.483.4050
> jacksonwhelan.com <http://jacksonwhelan.com?s=e> / jw at jacksonwhelan.com 
> <mailto:jw at jacksonwhelan.com>
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers 



More information about the wp-hackers mailing list