有关保存动态Metabox的问题

时间:2013-09-24 作者:Scott Baker

因此,我对动态待办事项列表元框存在问题。它应该很简单,除了储蓄,其他一切都在起作用。我已经看了5个小时了,需要一些新鲜的眼睛。

    function webm_todo_box(){
    global $post;
    ?>
    <div id="meta_inner">
    <?php

    //get the saved meta as an arry
    $TodoList = get_post_meta($post->ID,\'TodoList\');

    $c = 0;
    if ( is_array($TodoList) ) {
        foreach( $TodoList as $Todo ) {
            if ( isset( $TodoList[\'todotitle\'] ) ) {
                printf( \'<p>Todo Item Title: <input type="text" name="TodoList[%1$s][todotitle]" value="%2$s" /><span class="remove">%4$s</span></p>\', $c, $TodoList[\'todotitle\'], __( \'Remove Track\' ) );
                $c = $c +1;
            }
        }
    }

    ?>
<span id="here"></span>
<span class="add"><?php _e(\'Add Todo Item\'); ?></span>
<script>
    var $ =jQuery.noConflict();
    $(document).ready(function() {
        var count = <?php echo $c; ?>;
        $(".add").click(function() {
            count = count + 1;

            $(\'#here\').append(\'<p>Todo Item Title: <input type="text" name="TodoList[\'+count+\'][todotitle]" value="" /><span class="remove">Remove Track</span></p>\' );
            return false;
        });
        $(".remove").live(\'click\', function() {
            $(this).parent().remove();
        });
    });
    </script>
</div><?php

}

add_action( \'save_post\', \'save_TodoList\', 10, 2 );
/* When the post is saved, saves our custom data */
function save_TodoList( $post_id ) {
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
        return;

    // OK, we\'re authenticated: we need to find and save the data

    $TodoList = $_POST[\'todotitle\'];


    update_post_meta($post_id,\'todotitle\', $TodoList);
}

2 个回复
SO网友:Maruti Mohanty

我也遇到过这个奇怪的问题,我不知道为什么$post_id 不包含帖子id。请尝试下面的代码段,这样就可以了

使用global $post 和使用$post->ID 在里面update_post_meta

add_action( \'save_post\', \'save_TodoList\' );
/* When the post is saved, saves our custom data */
function save_TodoList() {
    global $post;

    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
        return;

    // OK, we\'re authenticated: we need to find and save the data

    $TodoList = $_POST[\'todotitle\'];

    update_post_meta($post->ID,\'todotitle\', $TodoList); 
}

SO网友:Horttcore

在输入字段中,name属性为“TodoList…“我会说”$_POST[\'todotitle\']“只是空的

add_action( \'save_post\', \'save_TodoList\', 10, 2 );
/* When the post is saved, saves our custom data */
function save_TodoList( $post_id ) {
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
        return;

    // OK, we\'re authenticated: we need to find and save the data

    $TodoList = $_POST[\'todotitle\'];

    update_post_meta($post_id,\'todotitle\', $TodoList);

    # DEBUG START
    echo \'<pre>\' . print_r( $_POST, TRUE ) . \'</pre>\';
    die(\'---end---\');
    # DEBUG END
}
首先调试您正在做的事情。

检查函数是否实际被调用,检查是否通过了“DOING\\u AUTOSAVE”子句,检查是否设置了$\\u POST var,检查var是否包含预期的数据,保存数据

结束