WordPress – How can I hide the_meta output if it is empty?

I’ve created a function and hook to insert some custom field information on some of my posts.

<?php the_meta(); ?>

I have added some CSS formatting (box with background) for display. Problem: if I have a post without any custom fields defined it displays an empty box. How can I prevent it from outputting the empty if there is nothing to display? All I can find is information on specific field types and and can’t extrapolate from it. I’m definitely not a php boss.

Related posts

Leave a Reply

1 comment

  1. You can use get_post_custom() to get the custom fields as an array, and then do your output only if there are any custom fields in the array.

    This should do the job, though it’s not very elegant:

    $has_custom = false;
    foreach(get_post_custom_keys() as $k => $v) {
        $t = trim($v);
        if('_' != $t{0}) {
            $has_custom = true;
            break;
        }
    }
    if($has_custom) {
        the_meta();
    }