请在我的函数中考虑以下代码。php文件:
add_filter( \'attachment_fields_to_edit\', function($form_fields, $post){
$post_mime_type = (
get_post_mime_type( $post ) == \'application/vnd.openxmlformats-officedocument.wordprocessingml.document\' ||
get_post_mime_type( $post ) == \'application/msword\'
) ? 1 : 0;
if ( $post_mime_type == 1) {
$ilm = get_post_meta( $post->ID, \'is_latest\', true );
$checked = ($ilm == "1" ? \'checked="checked"\' : \'\');
$form_fields[\'is_latest\'] = array(
\'label\' => \'<b">Latest?</b>\',
\'input\' => \'html\',
\'html\' => "<input type=\\"checkbox\\"
name=\\"attachments[{$post->ID}][is_latest]\\"
id=\\"attachments[{$post->ID}][is_latest]\\"
value=\\"1\\" {$checked}/><br />");
return $form_fields;
}
}, null, 2 );
add_filter("attachment_fields_to_save", function($post, $attachment){
if($attachment[\'is_latest\'] == "1") {
$args = array( \'post_type\' => \'attachment\');
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $a ) {
update_post_meta($a->ID, \'is_latest\', 0);
}
}
update_post_meta($post[\'ID\'], \'is_latest\', 1);
} else {
update_post_meta($post[\'ID\'], \'is_latest\', 0);
}
}, null , 2);
add_filter("wp_ajax_save-attachment-compat", function(){
$post_id = $_POST[\'id\'];
if(($_POST[\'attachments\'][$post_id][\'is_latest\']) == "1") {
$args = array( \'post_type\' => \'attachment\');
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $a ) {
update_post_meta($a->ID, \'is_latest\', 0);
}
}
update_post_meta($post_id, \'is_latest\', 1);
} else {
update_post_meta($post_id, \'is_latest\', 0);
}
clean_post_cache($post_id);
}, 1, 0);
我想做的是允许上传多个Word文档,但让用户选择“最新”版本。通过在附件上使用自定义元字段,此代码可以很好地实现这一点。然而,在管理界面中,Wordpress喜欢通过AJAX进行保存,AJAX保存但不重新加载其他附件元,这是必需的功能。所以现在我真的想
wp_ajax_save-attachment-compat
筛选,运行AJAX请求以更新视图中的附件。我找到了AJAX操作“查询附件”,然后在JS方法中使用它
Attachments.sync
, 但我真的很难找到一种方法来把它们联系在一起?
干杯