保存Metabox可重复字段

时间:2013-01-23 作者:Daniel

这是我正在改编的代码https://gist.github.com/2057532. 问题是我无法保存我创建的新字段中的内容。我已经检查了这里的其他问题,但没有一个对我有帮助。知道问题出在哪里吗?

add_action(\'admin_init\', \'slider_metabox_caption\', 1);
function slider_metabox_caption() {
add_meta_box( \'repeatable-fields\', \'Captions\', \'repeatable_metabox_display\', \'slide\', \'normal\', \'high\'); }

function repeatable_metabox_display() {
global $post;

$repeatable_fields = get_post_meta($post->ID, \'repeatable_fields\', true);
wp_nonce_field( \'repeatable_meta_box_nonce\', \'repeatable_meta_box_nonce\' ); ?>

<script type="text/javascript">
    jQuery(document).ready(function($) {
        $(\'.metabox_submit\').click(function(e) {
            e.preventDefault();
            $(\'#publish\').click();
        });
        $(\'#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;
        });

        $(\'#repeatable-fieldset-one tbody\').sortable({
            opacity: 0.6,
            revert: true,
            cursor: \'move\',
            handle: \'.sort\'
        });
    });
</script>

<table id="repeatable-fieldset-one" width="100%">
    <thead>
        <tr>
            <th width="1%"></th>
            <th width="15%">Text</th>
            <th width="4%">Fx</th>
            <th width="4%">Easing</th>
            <th width="4%">Speed</th>
            <th width="4%">X</th>
            <th width="4%">Y</th>
            <th width="4%">Width</th>
            <th width="4%">Height</th>
            <th width="1%"></th>
        </tr>
    </thead>

    <tbody>

    <?php 
    if ( $repeatable_fields ) :
        foreach ( $repeatable_fields as $field ) { ?>

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

        <td><input type="text" class="widefat" name="text[]" value="<?php if($field[\'text\'] != \'\') echo esc_attr( $field[\'text\'] ); ?>" /></td>
        <td><input type="text" class="widefat" name="fx[]" value="<?php if($field[\'fx\'] != \'\') echo esc_attr( $field[\'fx\'] ); ?>" /></td>
        <td><input type="text" class="widefat" name="easing[]" value="<?php if($field[\'easing\'] != \'\') echo esc_attr( $field[\'easing\'] ); ?>" /></td>
        <td><input type="text" class="widefat" name="speed[]" value="<?php if($field[\'speed\'] != \'\') echo esc_attr( $field[\'speed\'] ); ?>" /></td>
        <td><input type="text" class="widefat" name="x[]" value="<?php if($field[\'x\'] != \'\') echo esc_attr( $field[\'x\'] ); ?>" /></td>
        <td><input type="text" class="widefat" name="y[]" value="<?php if($field[\'y\'] != \'\') echo esc_attr( $field[\'y\'] ); ?>" /></td>
        <td><input type="text" class="widefat" name="width[]" value="<?php if($field[\'width\'] != \'\') echo esc_attr( $field[\'width\'] ); ?>" /></td>
        <td><input type="text" class="widefat" name="height[]" value="<?php if($field[\'height\'] != \'\') echo esc_attr( $field[\'height\'] ); ?>" /></td>

        <td><a class="sort">|||</a></td>

    </tr>
    <?php } else : 
    // show a blank one ?>

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

        <td><input type="text" class="widefat" name="text[]" /></td>
        <td><input type="text" class="widefat" name="fx[]" /></td>
        <td><input type="text" class="widefat" name="easing[]" /></td>
        <td><input type="text" class="widefat" name="speed[]" /></td>
        <td><input type="text" class="widefat" name="x[]" /></td>
        <td><input type="text" class="widefat" name="y[]" /></td>
        <td><input type="text" class="widefat" name="width[]" /></td>
        <td><input type="text" class="widefat" name="height[]" /></td>

        <td><a class="sort">|||</a></td>

    </tr>

    <?php endif; ?>

    <!-- empty hidden one for jQuery -->
    <tr class="empty-row screen-reader-text">
        <td><a class="button remove-row" href="#">-</a></td>

        <td><input type="text" class="widefat" name="text[]" /></td>        
        <td><input type="text" class="widefat" name="fx[]" /></td>
        <td><input type="text" class="widefat" name="easing[]" /></td>
        <td><input type="text" class="widefat" name="speed[]" /></td>
        <td><input type="text" class="widefat" name="x[]" /></td>
        <td><input type="text" class="widefat" name="y[]" /></td>
        <td><input type="text" class="widefat" name="width[]" /></td>
        <td><input type="text" class="widefat" name="height[]" /></td>

        <td><a class="sort">|||</a></td>

    </tr>
    </tbody>
</table>

<p><a id="add-row" class="button" href="#">Add another</a>
<input type="submit" class="metabox_submit" value="Save" />
</p>

<?php
}

add_action(\'save_post\', \'repeatable_meta_box_save\');
function repeatable_meta_box_save($post_id) {
if ( ! isset( $_POST[\'repeatable_meta_box_nonce\'] ) ||
    ! wp_verify_nonce( $_POST[\'repeatable_meta_box_nonce\'], \'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();

$text = $_POST[\'text\'];
$fx = $_POST[\'fx\'];
$easing = $_POST[\'easing\'];
$speed = $_POST[\'speed\'];
$x = $_POST[\'x\'];
$y = $_POST[\'y\'];
$width = $_POST[\'width\'];
$height = $_POST[\'height\'];

$count = count( $text );

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

    elseif ( $fx[$i] != \'\' ) :
        $new[$i][\'fx\'] = stripslashes( strip_tags( $fx[$i] ) );

    elseif ( $easing[$i] != \'\' ) :
        $new[$i][\'easing\'] = stripslashes( strip_tags( $easing[$i] ) );

    elseif ( $speed[$i] != \'\' ) :
        $new[$i][\'speed\'] = stripslashes( strip_tags( $speed[$i] ) );

    elseif ( $x[$i] != \'\' ) :
        $new[$i][\'x\'] = stripslashes( strip_tags( $x[$i] ) );

    elseif ( $y[$i] != \'\' ) :
        $new[$i][\'y\'] = stripslashes( strip_tags( $y[$i] ) );

    elseif ( $width[$i] != \'\' ) :
        $new[$i][\'width\'] = stripslashes( strip_tags( $width[$i] ) );

    elseif ( $height[$i] != \'\' ) :
        $new[$i][\'height\'] = stripslashes( strip_tags( $height[$i] ) );

    endif;
}

if ( !empty( $new ) && $new != $old )
    update_post_meta( $post_id, \'repeatable_fields[$i]\', $new );
elseif ( empty($new) && $old )
    delete_post_meta( $post_id, \'repeatable_fields[$i]\', $old );
}

2 个回复
最合适的回答,由SO网友:Daniel 整理而成

使用以下代码可以检索数组。

foreach ($repeatable_fields as $v) {

}   
然后您可以通过以下方式访问单个值:

<?php echo $v[\'text\']; ?>

SO网友:Bainternet

可能是您保存错误,请更改以下内容:

if ( !empty( $new ) && $new != $old )
    update_post_meta( $post_id, \'repeatable_fields[$i]\', $new );
elseif ( empty($new) && $old )
    delete_post_meta( $post_id, \'repeatable_fields[$i]\', $old );
对此:

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 );

结束

相关推荐

如何根据登录的用户有不同的Header.php?

我已经使用wishlist成员插件创建了wordpress成员网站。但我想要不同的header.php 基于不同的登录名。用户包括:打印、网络和免费免费用户登录时–我想要header1.php 当打印用户登录时,加载并保留直到他注销header2.php 当Web用户登录时,加载并保留直到他注销header3.php 如果没有人登录默认设置,则加载并保留直到他注销header.php 保持不变。请协助完成此操作。你好Raghav。