Output from Meta Box Array

时间:2012-02-15 作者:andy rob

我已经创建了一个元框,它成功地存储了post数据,但我在输出内容并以合理的方式显示它时遇到了困难,这超出了我对PHP的有限知识。我是一个养蜂人,我想记录我的蜂箱的检查情况,所以每个蜂箱都有一个记录,其中包含一定数量的字段。

因此,我想遍历所有配置单元的colony数组,即直到$hive为空,然后以每个配置单元有一行的表的形式输出数据。目前,我已经成功地手动生成了表的第一行,但不知道如何设置一个循环来完成这项工作。

<?php if (have_posts() ):?>

        <?php while ( have_posts() ) :the_post();?>
    <?php
    $meta=$simple_mb->the_meta();
     // need a loop here until all instances colony are reached 
        $hive = $meta[\'colony\'] [0][\'hive\']; 
                $queen = $meta[\'colony\'] [0][\'cb_ex4_cb\'];
        $bias = $meta[\'colony\'] [0][\'cb_ex4_bias\'];
        $brood = $meta[\'colony\'] [0][\'cb_ex4_bcom\'];
        $stores = $meta[\'colony\'] [0][\'stores\'];
        $mycomm = $meta[\'colony\'] [0][\'comment\'];
        $plan = $meta[\'colony\'] [0][\'plan\'];
    ?>

            <table id="anytable" summary="Colony Inspections" cellspacing="0">
            <caption>        < ?php echo   $inspect;?> 
            <?php       the_title(); ?></caption>           
            <tbody>

    <tr>        <!-- want additional rows added for each colony -->
    <th scope="row"><?php echo $hive; ?></th> 
    <td><div><?php echo $queen; ?></div></td>
    <td><div><?php echo $brood; ?></div></td>
    <td><div><?php echo $stores; ?></div></td>
    <td><?php echo $mycomm; ?></td>
    <td><?php echo $plan; ?></td>
    </tr>
    </tbody>
    </table>


    <?php endwhile; ?>

    <?php endif; ?>

2 个回复
SO网友:kaiser

将元数据作为“平面”数据添加到帖子中实际的问题是,在调用get_post_custom( $id ); 为您的帖子。这里有一个简单的函数,它将附加到post的所有元数据添加到post对象。

/**
 * Merges custom/meta data with post data
 * 1) get_post_custom() is cached from the WP_Query
 * 2) The following line gets the first element from each meta array as it\'s only a single value
 * It then merges the resulting flattened Array with the post object that gets casted to another Array
 * 
 * @param array $query
 * @return array result 
 */
public function add_meta_data( $query )
{
    foreach ( $query as $post )
        $result[] = array_merge( 
             array_map( \'array_shift\', get_post_custom( $post->ID ) )
            ,(array) $post 
        );

    return $result;
}
然后,我们简单地用<table>. 您可以添加<thead><th>etc. 之前和<tfoot>etc. 之后那就把你所有的<tr><td>Cell</td>etc. 在回路内部。

// Before your loop:
global $wp_query;
// Merge the queried posts with their meta data, using our custom template tag/function
$query = add_meta_data( $wp_query->posts );

echo "<table>";
// Loop through our new query array
foreach ( $query as $post )
{
        // This extracts your post object, so you don\'t have to save them in separate vars
        // @see @link http://php.net/manual/de/function.extract.php
        extract( $post, EXTR_SKIP );

        echo "<tr>";
        // Then simply call all your meta data like this:
        echo "<td>{$hive}</td>";
        // Then add other meta data here:
        echo "<td>{$whatever}</td>";
        echo "</tr>";
}
echo "</table>";

SO网友:rg89

你可以试试这样的。。。

// need a loop here until all instances colony are reached 

foreach( $meta[\'colony\'] as $colony ){
    echo \'<tr>\';
    foreach( $colony as $key => $value ){
        //$key may contain \'cb_ex4_cb\'
        //$value may contain what you put in $queen
        echo \'<td>$value</td>\'; 
    }
    echo \'</tr>\';
}

结束

相关推荐

从Metabox将上传的图像添加到编辑器,而不是默认的弹出式上载程序

我需要找到插件或创建在自定义元数据库中的一些帖子的所有上传图像列表。我可以单击缩略图,以自定义格式将图像添加到编辑器中。我下载了很多照片到默认的上传框,每次我都会点击很多次,以便在编辑器中插入新图像。这很不舒服。