Something like this: $ck = get_post_custom_keys($post_id); //Array // drop keys starting with '_' $ck = array_filter($ck, function($key){ return strpos($key, '_') !== 0; }); // store your root keys here $data = array(); foreach($ck as $k){ $cv = get_post_custom_values($k, $post_id ); //Array // drop empty values $cv = array_filter($cv); if($cv) $data[$k] = $cv; } if($data){ // your html here; iterate over $data $html = ''; foreach($data as $key => $contents) $html .= sprintf('<h4>%s</h4><div class="customfield-content"><p>%s</p></div>', esc_attr($key), implode('</p><p>', array_map('esc_attr', $contents))); printf('<div class="customfield-box">%s</div>', $html); }else{ // nothing }