显示可重复的Meta Box内容

时间:2016-12-06 作者:Trenton Moore

我在函数中使用以下可重复的元框代码。php:

    /**
 * Repeatable Custom Fields in a Metabox
 * Author: Helen Hou-Sandi
 *
 * From a bespoke system, so currently not modular - will fix soon
 * Note that this particular metadata is saved as one multidimensional array (serialized)
 */

function hhs_get_sample_options() {
    $options = array (
        \'Option 1\' => \'option1\',
        \'Option 2\' => \'option2\',
        \'Option 3\' => \'option3\',
        \'Option 4\' => \'option4\',
    );

    return $options;
}
add_action(\'admin_init\', \'hhs_add_meta_boxes\', 1);
function hhs_add_meta_boxes() {
    add_meta_box( \'repeatable-fields\', \'Emergency Contacts\', \'hhs_repeatable_meta_box_display\', \'team\', \'normal\', \'default\');
}
function hhs_repeatable_meta_box_display() {
    global $post;
    $repeatable_fields = get_post_meta($post->ID, \'repeatable_fields\', true);
    $options = hhs_get_sample_options();
    wp_nonce_field( \'hhs_repeatable_meta_box_nonce\', \'hhs_repeatable_meta_box_nonce\' );
    ?>
    <script type="text/javascript">
    jQuery(document).ready(function( $ ){
        $( \'#add-row\' ).on(\'click\', function() {
            var row = $( \'.empty-row.screen-reader-text\' ).clone(true);
            row.removeClass( \'empty-row screen-reader-text\' );
            row.insertBefore( \'#repeatable-fieldset-one tbody>tr:last\' );
            return false;
        });

        $( \'.remove-row\' ).on(\'click\', function() {
            $(this).parents(\'tr\').remove();
            return false;
        });
    });
    </script>

    <table id="repeatable-fieldset-one" width="100%">
    <thead>
        <tr>
            <th width="40%">Name</th>
            <th width="40%">Phone Number</th>
            <th width="8%"></th>
        </tr>
    </thead>
    <tbody>
    <?php

    if ( $repeatable_fields ) :

    foreach ( $repeatable_fields as $field ) {
    ?>
    <tr>
        <td><input type="text" class="widefat" name="name[]" value="<?php if($field[\'name\'] != \'\') echo esc_attr( $field[\'name\'] ); ?>" /></td>

        <td><input type="text" class="widefat" name="phone[]" value="<?php if ($field[\'phone\'] != \'\') echo esc_attr( $field[\'phone\'] ); else echo \'http://\'; ?>" /></td>

        <td><a class="button remove-row" href="#">Remove</a></td>
    </tr>
    <?php
    }
    else :
    // show a blank one
    ?>
    <tr>
        <td><input type="text" class="widefat" name="name[]" /></td>

        <td><input type="text" class="widefat" name="phone[]" value="" /></td>

        <td><a class="button remove-row" href="#">Remove</a></td>
    </tr>
    <?php endif; ?>

    <!-- empty hidden one for jQuery -->
    <tr class="empty-row screen-reader-text">
        <td><input type="text" class="widefat" name="name[]" /></td>

        <td><input type="text" class="widefat" name="phone[]" value="" /></td>

        <td><a class="button remove-row" href="#">Remove</a></td>
    </tr>
    </tbody>
    </table>

    <p><a id="add-row" class="button" href="#">Add another</a></p>
    <?php
}
add_action(\'save_post\', \'hhs_repeatable_meta_box_save\');
function hhs_repeatable_meta_box_save($post_id) {
    if ( ! isset( $_POST[\'hhs_repeatable_meta_box_nonce\'] ) ||
    ! wp_verify_nonce( $_POST[\'hhs_repeatable_meta_box_nonce\'], \'hhs_repeatable_meta_box_nonce\' ) )
        return;

    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
        return;

    if (!current_user_can(\'edit_post\', $post_id))
        return;

    $old = get_post_meta($post_id, \'repeatable_fields\', true);
    $new = array();
    $options = hhs_get_sample_options();

    $names = $_POST[\'name\'];
    $phones = $_POST[\'phone\'];

    $count = count( $names );

    for ( $i = 0; $i < $count; $i++ ) {
        if ( $names[$i] != \'\' ) :
            $new[$i][\'name\'] = stripslashes( strip_tags( $names[$i] ) );

            if ( $phones[$i] == \'\' )
                $new[$i][\'phone\'] = \'\';
            else
                $new[$i][\'phone\'] = stripslashes( $phones[$i] ); // and however you want to sanitize
        endif;
    }
    if ( !empty( $new ) && $new != $old )
        update_post_meta( $post_id, \'repeatable_fields\', $new );
    elseif ( empty($new) && $old )
        delete_post_meta( $post_id, \'repeatable_fields\', $old );
}

add_action( \'init\', \'team_contacts_posttype\' );
我只需要知道如何分别显示每件作品。我在前端尝试了以下方法以及其他一些方法,但都没有成功:

<?php
        $member_contact_meta = get_post_meta(get_the_ID(), \'tcp\', true);
        $emergency_contact_meta = get_post_custom(get_the_ID(), \'repeatable_fields\', true);
        $emerency_contact_name = $emergency_contact_meta[\'name\'];
        $emergency_contact_phone = $emergency_contact_meta[\'phone\'];
        the_content();
        echo $emergency_contact_meta;
        echo \'<strong>Phone: </strong>\' . $member_contact_meta . \'<br />\';
        echo \'<strong>Emergency Contacts:</strong><br />\';
        foreach ($emergency_contact_meta as $emergency_contact_metas)
        echo $emergency_contact_name . \' - \' . $emergency_contact_phone;
    ?>
tcp元是独立的,工作正常,但我不知道如何获取重复的内容元并显示它。

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

下面是模板代码的测试和工作版本,其中显示了可重复的$emergency_contact_meta 正确:

$member_contact_meta = get_post_meta( get_the_ID(), \'tcp\', true );
echo \'<strong>Phone: </strong>\' . $member_contact_meta . \'<br />\';

$emergency_contact_meta = get_post_meta( get_the_ID(), \'repeatable_fields\', true);

if ( $emergency_contact_meta ) {
    echo \'<strong>Emergency Contacts:</strong><br />\';

    foreach ( $emergency_contact_meta as $emergency_contact_metas ) {
        echo \'<strong>\' . esc_html( $emergency_contact_metas[\'name\'] ) .\'</strong> \' . esc_html( $emergency_contact_metas[\'phone\'] ) . \'<br />\';
    }
}
原始代码中的主要问题是get_post_custom() 当它出现时,你只是想获取可重复字段的元数据,repeatable_fields. get_post_custom() 获取all 特定帖子的元字段。

相关推荐

如何在WordPress开发中添加带有ACF自定义字段ID的自定义metabox字段

我是wordpress开发的新手,我在我的项目中安装了高级自定义字段插件,并创建了两个文本字段名称&;我还创建了一个插件,可以在帖子中创建一个带有文本框的元框。现在在帖子中,我将获得自定义字段名称(&A);电子邮件和我的自定义元框旁边将出现,但我必须将我的元框附加到名称字段旁边,即在名称字段和电子邮件字段之间。我的metabox代码如下。请任何人帮帮我//Creating the custom meta box function my_notice_meta_box() {