Skip to main content
added 262 characters in body
Source Link
onetrickpony
  • 13.6k
  • 7
  • 58
  • 87

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
}
       

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

}else{
  // nothing
}
       

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
}
       
Source Link
onetrickpony
  • 13.6k
  • 7
  • 58
  • 87

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

}else{
  // nothing
}