将元框中的自定义字段分组在一起,以便在网格中显示为行


Group Custom Fields in meta boxes together for displaying as rows in a grid?

我为产品制作了一个自定义的帖子类型。它将有10个自定义字段类型,如:

DocName1
DocUrl1

DocName2
DocUrl2

…等等......以下是用于自定义字段的自定义post类型元框的代码:

//* Add custom Meta Boxes for Products *//
$prefix = 'aps_';  //To prevent conflicts with other plugins
$meta_box = array(
    'id' => 'products-meta-boxes',
    'title' => "Product Details",
    'page' => 'tf_products',  //attach to products custom post
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'Document Name 1',
            'desc' => 'Name of PDF or Document you want to share',
            'id' => $prefix . 'docname1',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Document URL 1',
            'desc' => 'Web Address to PDF or document you want to share',
            'id' => $prefix . 'docurl1',
            'type' => 'text',
            'std' => 'http://'
        ),
        array(
            'name' => 'Document Name 2',
            'desc' => 'Name of PDF or Document you want to share',
            'id' => $prefix . 'docname2',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Document URL 2',
            'desc' => 'Web Address to PDF or document you want to share',
            'id' => $prefix . 'docurl2',
            'type' => 'text',
            'std' => 'http://'
        )       
    )   
);

我想把它们像DocName1 - DocUrl1一样组合在一起,这样它们就可以在网格的单行上作为文本框发出回声。我有一个网格准备在我的自定义帖子类型添加/编辑表单,我想把文本框,所以他们可以添加或编辑。截图在这里https://i.stack.imgur.com/ZGqGI.png

我可以很容易地做一个foreach ($meta_box['fields'] as $field)和echo出每个文本框,但这是为每个字段,而不是一个组(如DocName1和DocUrl1),但我想要DocName1 - DocUrl1在同一网格线上。有办法做到这一点吗?我想不出一个有效的方法来做这件事。

我现在是这样做的:

foreach ($meta_box['fields'] as $field) {
    // get current post meta data
    $meta = get_post_meta($post->ID, $field['id'], true);
    echo '<tr>',
           '<th style="width:20%"><label for="', $field['id'], '">',  $field['name'], '</label></th>',
               '<td>';
    echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30"                    style="width:97%" />', '<br />', $field['desc'];
    echo '</td>',
        '</tr>';
}

但是,当然,这个回显是在单独的行上输出每个字段。我想要一个网格,在第一个网格线上有DocName1和DocUrl1,然后在第二个网格线上有DocName2和DocUrl2,等等。

对不起,如果这是混淆

我想我自己回答了这个问题!

我在show meta box函数中创建了一个for循环,如下所示:

//Show Meta Fields
function products_show_meta() {
global $meta_box, $post, $prefix;
// Use nonce for verification
echo '<input type="hidden" name="products_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="widefat">',
        '<thead>',
            '<tr>',
                '<th style="width:30%">Document Name</th>',
                '<th>Document URL</th>',
            '</tr>',
        '<thead>',
        '<tfoot>',
            '<tr>',
                '<th>Document Name</th>',
                '<th>Document URL</th>',
            '</tr>',
        '<tfoot>';
echo    '<tbody>';
    for ($i=1; $i <= count($meta_box["fields"])/2; $i++) { 
        $current_docName = $prefix . 'docname' . $i;
        $current_docUrl = $prefix . 'docurl' . $i;
        $meta_docName = get_post_meta($post->ID, $current_docName, true);
        $meta_docUrl = get_post_meta($post->ID, $current_docUrl, true);
        echo '<tr>',
                '<td>',
                    '<input type="text" name="', $current_docName, '" id="', $current_docName, '" value="', $meta_DocName ? $meta_DocName                           : '', '" size="30" style="width:99%" />',
                '</td>',
                '<td>',
                    '<input type="text" name="', $current_docUrl, '" id="', $current_docUrl, '" value="', $meta_DocUrl ? $meta_DocUrl :                             'http://', '" size="30" style="width:99%" />',
                '</td>',
            '</tr>';             
    }
echo    '</tbody>',
    '</table>';

首先回显表的页眉和页脚,为每列指定适当的名称。然后我运行一个从1开始的for循环,并计算我拥有的字段数量(我必须将其除以2,否则它会将行加倍)。然后我只需要获取每个字段并回显该行。这是下面的代码渲染:https://i.stack.imgur.com/NPu66.png它的工作和保存数据完美!