显示带有值的自定义字段的Metabox标题

时间:2011-12-12 作者:at least three characters

我在写文章时使用了多个metabox面板。在我的函数中,我注册了多个元数据库:

$prefix = \'dbt_\';

$meta_boxes = array();

$meta_boxes[] = array(
    \'id\' => \'general_information\',
    \'title\' => \'General Information\',
    \'pages\' => array(\'post\', \'page\', \'link\'), // multiple post types, accept custom post types
    \'context\' => \'normal\', // normal, advanced, side (optional)
    \'priority\' => \'high\', // high, low (optional)
    \'fields\' => array(
        array(
            \'name\' => \'Name\',
            \'id\' => $prefix . \'name\',
            \'type\' => \'text\',
            \'std\' => \'default value here\'
        ),
        array(
            \'name\' => \'Manufacturer\',
            \'id\' => $prefix . \'manufacturer\',
            \'type\' => \'text\',
            \'std\' => \'\'
        )

$meta_boxes[] = array(
    \'id\' => \'measurements\',
    \'title\' => \'Measurements\',
    \'pages\' => array(\'post\', \'page\', \'link\'), // multiple post types, accept custom post types
    \'context\' => \'normal\', // normal, advanced, side (optional)
    \'priority\' => \'high\', // high, low (optional)
    \'fields\' => array(
        array(
            \'name\' => \'Length\',
            \'id\' => $prefix . \'length\',
            \'type\' => \'text\',
            \'std\' => \'\'
        ),
        array(
            \'name\' => \'Width\',
            \'id\' => $prefix . \'width\',
            \'type\' => \'text\',
            \'std\' => \'\'
        )

..etc
在我的主题中,我将显示每个metabox组中每个字段的名称和值。目标是让字段按元盒分组,否则我只会使用get_post_custom($post_id).

单一代码。php:

global $meta_boxes;

foreach ( $meta_boxes as $metabox ) {
echo $metabox[\'title\'];
  foreach ( $metabox[\'fields\'] as $field ) {
    $meta = get_post_meta($post->ID, $field[\'id\'], true); //get post meta from each metabox
    if(!isset($meta[0])) continue; //display only fields with values
    echo $field[\'name\'];
    echo $meta ? $meta : $field[\'std\']; //show value or default value
  }
}
例如,如果元框没有带值的字段,则元框标题$metabox[\'title\'] 仍然显示。如果不存在值,我如何隐藏metabox标题?

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

您可以添加另一个简单的foreach循环,以首先检查是否有任何值:

foreach ( $meta_boxes as $metabox ) {
    $has_value = false
    foreach ( $metabox[\'fields\'] as $field ) {
        $meta = get_post_meta($post->ID, $field[\'id\'], true); 
        if(!isset($meta[0])){
            $need_title = true;
            break;
        }
    }
    if ($has_value){
        echo $metabox[\'title\'];
        foreach ( $metabox[\'fields\'] as $field ) {
            $meta = get_post_meta($post->ID, $field[\'id\'], true); //get post meta from each metabox
            if(!isset($meta[0])) continue; //display only fields with values
            echo $field[\'name\'];
            echo $meta ? $meta : $field[\'std\']; //show value or default value
        }
    }
}

结束

相关推荐

404 for index.php

我最近在IIS7上从linux切换到windows主机。我的permalinks工作得很好,但由于某种原因,当我尝试查看站点索引(index.php)时,我得到了一个内部wordpress 404。我只能假设这与迁移有关,但我对原因感到困惑。我的网站。配置包含以下内容:<?xml version=\"1.0\" encoding=\"UTF-8\"?> <configuration> <system.webServer>