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

I have a custom post type I made for Products. It will have 10 custom field types like so:

DocName1
DocUrl1

Read More

DocName2
DocUrl2

… and so on. Here’s the code for the custom post type meta boxe for the custom fields:

//* 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://'
        )       
    )   
);

I’d like to group them together like DocName1 - DocUrl1 so they can be echo’d out on a single line of a grid as textboxes. I’ve got a grid ready on my custom post type add/edit form that I want to put textboxes in so they can be added or edited. Screenshot here http://i.stack.imgur.com/ZGqGI.png

I can easily do a foreach ($meta_box['fields'] as $field) and echo out the text box for each one, but that is for EACH FIELD, not a group (like DocName1 and DocUrl1), but I want DocName1 - DocUrl1 on the same grid line. Is there a way to do this? I can’t wrap my head around an efficient way to do this.

The way I’m doing it now is like so:

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>';
}

But of course this echo’s out each field on its own line. I want a grid with DocName1 and DocUrl1 on the first gridline, then DocName2 and DocUrl2 on the second, and so on.

Sorry if this is confusing.

Related posts

Leave a Reply

1 comment

  1. I think I answered this one on my own!

    I created a for loop in my show meta box function like so:

    //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>';
    

    First I echo out the header and footer of the table, giving each column the appropriate name. Then I run a for loop that starts at 1 and counts the number of fields I have ( I have to divide it by 2 otherwise it doubles up the rows). Then I just grab each field and echo out the row. This is what the code below renders: http://i.stack.imgur.com/NPu66.png It’s working and saving the data perfectly!