WordPress:可排序的Metabox字段不保存位置

时间:2013-08-28 作者:luxo

我遇到了一个让我困惑的问题:升级到WP 3.6后,当您重新订购时,我的可排序metabox字段没有保存它们的位置。下面是我的代码:

PHP:

function save_box( $post_id ) {
    $post_type = get_post_type();

    // verify nonce
    if ( ! isset( $_POST[\'custom_meta_box_nonce_field\'] ) )
        return $post_id;
    if ( ! ( in_array( $post_type, $this->page ) || wp_verify_nonce( $_POST[\'custom_meta_box_nonce_field\'],  \'custom_meta_box_nonce_action\' ) ) ) 
        return $post_id;
    // check autosave
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
        return $post_id;
    // check permissions
    if ( ! current_user_can( \'edit_page\', $post_id ) )
        return $post_id;

    // loop through fields and save the data
    foreach ( $this->fields as $field ) {
        if( $field[\'type\'] == \'section\' ) {
            $sanitizer = null;
            continue;
        }
        if( in_array( $field[\'type\'], array( \'tax_select\', \'tax_checkboxes\' ) ) ) {
            // save taxonomies
            if ( isset( $_POST[$field[\'id\']] ) ) {
                $term = $_POST[$field[\'id\']];
                wp_set_object_terms( $post_id, $term, $field[\'id\'] );
            }
        }
        else {
            // save the rest
            $old = get_post_meta( $post_id, $field[\'id\'], $field[\'type\'], true );
            if ( isset( $_POST[$field[\'id\']] ) )
                $new = $_POST[$field[\'id\']];
            if ( isset( $new ) && $new != $old ) {
                $sanitizer = isset( $field[\'sanitizer\'] ) ? $field[\'sanitizer\'] : \'sanitize_text_field\';
                if ( is_array( $new ) )
                    $new = meta_box_array_map_r( \'meta_box_sanitize\', $new, $sanitizer );
                else
                    $new = meta_box_sanitize( $new, $sanitizer );
                update_post_meta( $post_id, $field[\'id\'], $new );
            } elseif ( isset( $new ) && \'\' == $new && $old ) {
                delete_post_meta( $post_id, $field[\'id\'], $old );
            }
        }
    } // end foreach
}
jQuery:

$(\'.meta_box_repeatable tbody\').sortable({
    opacity: 0.6,
    revert: true,
    cursor: \'move\',
    handle: \'.hndle\'
});

// post_drop_sort   
$(\'.sort_list\').sortable({
    connectWith: \'.sort_list\',
    opacity: 0.6,
    revert: true,
    cursor: \'move\',
    cancel: \'.post_drop_sort_area_name\',
    items: \'li:not(.post_drop_sort_area_name)\',
    update: function(event, ui) {
        var result = $(this).sortable(\'toArray\');
        var thisID = $(this).attr(\'id\');
        $(\'.store-\' + thisID).val(result) 
    }
});

$(\'.sort_list\').disableSelection();
我有一个包含可重复框的元框,在可重复框内有一个图像上载按钮和标题文本输入。一切正常,我可以添加更多的可重复项并对其进行排序,但当我保存帖子时,可重复项会返回到其原始位置。

如果有人愿意,我可以提供您的登录详细信息,以查看实时版本。

任何帮助都将不胜感激。

1 个回复
SO网友:kaiser

通常只需将postsbox 在您希望具有可排序元框的页面上编写脚本。

只需执行以下操作:

add_action( \'admin_enqueue_scripts\', \'wpse112022_postbox_enqueue\' );
function wpse112022_postbox_enqueue( $hook_suffix )
{
    // uncomment the next line to see what the $hook_suffix of your desired page is
    # var_dump( $hook_suffix );
    if ( \'hook_suffix of desired page\' !== $hook_suffix )
        return;

    wp_enqueue_script( \'postbox\' );
    // If the last line didn\'t enqueue the script, then uncomment the following instead:
    /*
    $suffix = defined( \'SCRIPT_DEBUG\' ) && SCRIPT_DEBUG ? \'\' : \'.min\';
    wp_enqueue_script(
        \'postbox\',
        "/wp-admin/js/postbox{$suffix}.js",
        array( \'jquery-ui-sortable\' ),
        false
        true
    );
    */
}

结束

相关推荐

此jQuery代码片段似乎不适用于WordPress

我并不是一个真正的jQuery爱好者,所以我不太清楚为什么这个jQuery模态lightbox小片段不起作用。我是否缺少任何类型的WP特定代码?如果有帮助的话,我正在将脚本排入标题中$(window).load(function(){ var lightBox = $(\'#lightbox\'), lightBoxContent = $(\'#lb-content\'); var positionLightbox = function() {