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

发布于 2024-11-04 11:04:37 字数 2705 浏览 1 评论 0原文

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

DocName1
DocUrl1

DocName2
DocUrl2

...等等。以下是自定义字段的自定义帖子类型元框的代码:

//* 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.sstatic.net/ZGqGI.png

我可以轻松地执行 < code>foreach ($meta_box['fields'] as $field) 并回显每个字段的文本框,但这是针对每个字段,而不是一组(如 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,依此类推。

抱歉,如果这令人困惑。

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

DocName1
DocUrl1

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 https://i.sstatic.net/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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無心 2024-11-11 11:04:37

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

我在显示元框函数中创建了一个 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.sstatic.net/NPu66.png它正在完美地工作并保存数据!

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: https://i.sstatic.net/NPu66.png It's working and saving the data perfectly!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文