如果Metabox有内容显示内容

时间:2013-11-16 作者:Lasse M. Tvedt

如果字段有内容,我知道如何显示字段内容。

$monday = get_post_meta($post->ID, \'_cmb_open_monday\', true);
if ( ! empty ( $monday ) ) {
    echo \'content\';
}
如果元框“store\\u openingtimes”中的一个字段有内容,我如何显示内容?也许这更好地解释了我想做什么:

$storeopen = Get all meta from metabox id "store_openingtimes"
if ( ! empty ( $storeopen ) ) {
    echo \'content\';
}

1 个回复
最合适的回答,由SO网友:gmazzap 整理而成

您必须列出元框中保存的元字段的键,因为它们没有动态检索特定元框中保存的所有字段的功能。例如:

$store_openingtimes_keys = array(
  \'_cmb_open_monday\', \'_cmb_open_tuesday\', \'_cmb_open_wednesday\', \'_cmb_open_thursday\',
  \'_cmb_open_friday\', \'_cmb_open_saturday\', \'_cmb_open_sunday\'
);
// all the not empty meta for the post
$meta = array_filter( get_post_custom($post->ID) );
// there are any not empty meta that is one of the store opening times?
$times_meta = array_intersect( array_keys($meta), $store_openingtimes_keys );
if ( ! empty($times_meta) ) { // if so, show content
   echo $content;
}

结束