我不完全确定您对代码的意图是什么,不幸的是,我没有发现您提供的工作得很好,所以我重写了提供的部分代码,以使其成为更可行的解决方案。
此代码用于将附件订单保存在save
, 不是通过ajax,但实际上没有理由在每种类型上保存这些更改(有吗?)。
我重构了代码,以便您可以调整代码顶部的post类型。我不确定您使用enqueue调用的目的是什么,也很确定它不能以您的方式调用,也不需要在post editor页面上调用(可排序和必要的脚本已经在编辑器屏幕上可用/加载)。
试一试,看看你过得怎么样。。(请注意,这是一个工作示例)。。
// Not applicable to my testing, but left it in because i\'m sure it\'s appropriate to your usage
add_theme_support( \'post-thumbnails\' );
add_image_size( \'editor-thumb\', 130, 72, true );
// Quicker to update one line than several, only reason it\'s defined here
$my_post_type = \'book\';
// Add metabox on the proper metabox hook
add_action( \'add_meta_boxes_\' . $my_post_type, \'add_image_sortable_box\', 2000 );
// Fire jQuery only on the appliable pages
add_action( \'admin_footer-post.php\', \'add_sortable_to_elements\', 2000 );
add_action( \'admin_footer-post-new.php\', \'add_sortable_to_elements\', 2000 );
function add_image_sortable_box() {
global $my_post_type;
add_meta_box( \'test-image-thing\', \'Sortable Attachments Test\', \'do_image_metabox_thingy\', $my_post_type, \'side\', \'default\' );
}
function add_sortable_to_elements() {
?>
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function($) {
$(\'#attachmentcontainer\').sortable();
});
//]]>
</script>
<?php
}
function do_image_metabox_thingy( $p ) {
// No global statement needed here, the hook this function is attached to gives you the post object
$args = array(
\'order\' => \'asc\',
\'orderby\' => \'menu_order\',
\'post_type\' => \'attachment\',
\'post_parent\' => $p->ID,
\'post_mime_type\' => \'image\',
\'post_status\' => null,
\'numberposts\' => -1,
);
$attachments = get_posts( $args );
if( $attachments ) :
// Only need 1 nonce to cover the lot
wp_nonce_field( \'my_attachment_sort\', \'attachment_sort_nonce\' );
?>
<div class="imageuploader">
<div id="attachmentcontainer">
<?php
foreach( $attachments as $attachment ) :
$attachmentid = $attachment->ID;
$editorimage = wp_get_attachment_image_src( $attachment->ID, \'editor-thumb\', false, false);
?>
<div class="attachment" id="test-<?php echo $attachment->ID; ?>">
<div class="image">
<img width="100" height="auto" src="<?php echo $editorimage[0]; ?>" />
<input type="hidden" name="att_id[]" id="att_id" value="<?php echo $attachment->ID; ?>" />
</div>
</div>
<?php
endforeach;
?>
<div style="clear: both;"></div>
</div>
</div>
<?php
endif;
}
// Updates the attachments when saving
add_filter( \'wp_insert_post_data\', \'test_meta_save\', 1000, 2 );
function test_meta_save( $data, $_post_vars ) {
global $my_post_type;
if( $my_post_type != $data[\'post_type\'] || !isset( $_post_vars[\'attachment_sort_nonce\'] ) )
return $data;
if( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $data;
if( !wp_verify_nonce( $_post_vars[\'attachment_sort_nonce\'], \'my_attachment_sort\' ) )
return $data;
global $post_ID;
if( !current_user_can( \'edit_post\', $post_ID ) )
return $data;
if( isset( $_post_vars[\'att_id\'] ) ) {
foreach( $_post_vars[\'att_id\'] as $img_index => $img_id ) {
$a = array(
\'ID\' => $img_id,
\'menu_order\' => $img_index
);
wp_update_post( $a );
}
}
return $data;
}
当然,一定要切换帖子类型值,book是我的测试帖子类型:)
如果有任何问题,请将其添加到原始问题中,然后在此答案上留言让我知道。。