我已经创建了一个带有图像上传器的自定义元盒,它将多个图像上传到一个图库中,然后我可以查询。这一切都可以接受这样一个事实,即图像不会显示为媒体库中帖子的附件。它们显示为未连接。各位有什么想法吗?
<?php
// Add the Meta Box
function agch_properties_add_custom_meta_box() {
add_meta_box(
\'custom_meta_box\', // $id
\'Property Photos\', // $title
\'agch_properties_show_custom_meta_box\', // $callback
\'properties\', // $page
\'normal\', // $context
\'high\'); // $priority
}
add_action(\'add_meta_boxes\', \'agch_properties_add_custom_meta_box\');
// Field Array
$prefix = \'agch_properties_\';
$custom_meta_fields = array(
array(
\'label\'=> \'Upload Images\',
\'desc\' => \'This is the gallery images on the single item page.\',
\'id\' => $prefix.\'gallery\',
\'type\' => \'gallery\'
),
);
// The Callback
function agch_properties_show_custom_meta_box($object) {
global $custom_meta_fields, $post;
// Use nonce for verification
echo \'<input type="hidden" name="custom_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
// Begin the field table and loop
echo \'<table class="form-table">\';
foreach ($custom_meta_fields as $field) {
// get value of this field if it exists for this post
$meta = get_post_meta($post->ID, $field[\'id\'], true);
// begin a table row with
echo \'<tr>
<th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
<td>\';
switch($field[\'type\']) {
case \'gallery\':
$meta_html = null;
if ($meta) {
$meta_html .= \'<ul class="agch_properties_gallery_list">\';
$meta_array = explode(\',\', $meta);
foreach ($meta_array as $meta_gall_item) {
$meta_html .= \'<li><div class="agch_properties_gallery_container"><span class="agch_properties_gallery_close"><img id="\' . esc_attr($meta_gall_item) . \'" src="\' . wp_get_attachment_thumb_url($meta_gall_item) . \'"></span></div></li>\';
}
$meta_html .= \'</ul>\';
}
echo \'<input id="agch_properties_gallery" type="hidden" name="agch_properties_gallery" value="\' . esc_attr($meta) . \'" />
<span id="agch_properties_gallery_src">\' . $meta_html . \'</span>
<div class="agch_gallery_button_container"><input id="agch_properties_gallery_button" type="button" value="Add Images" /></div>\';
break;
} //end switch
echo \'</td></tr>\';
} // end foreach
echo \'</table>\'; // end table
}
// Save the Data
function agch_properties_save_custom_meta($post_id) {
global $custom_meta_fields;
// Verify nonce
if ($_POST && !wp_verify_nonce($_POST[\'custom_meta_box_nonce\'], basename(__FILE__)))
return $post_id;
// Check autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
return $post_id;
// Check permissions
if (\'properties\' == get_post_type()) {
if (!current_user_can(\'edit_page\', $post_id))
return $post_id;
} elseif (!current_user_can(\'edit_post\', $post_id)) {
return $post_id;
}
// Loop through meta fields
foreach ($custom_meta_fields as $field) {
$new_meta_value = esc_attr($_POST[$field[\'id\']]);
$meta_key = $field[\'id\'];
$meta_value = get_post_meta( $post_id, $meta_key, true );
// If theres a new meta value and the existing meta value is empty
if ( $new_meta_value && $meta_value == null ) {
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
// If theres a new meta value and the existing meta value is different
} elseif ( $new_meta_value && $new_meta_value != $meta_value ) {
update_post_meta( $post_id, $meta_key, $new_meta_value );
} elseif ( $new_meta_value == null && $meta_value ) {
delete_post_meta( $post_id, $meta_key, $meta_value );
}
}
}
add_action(\'save_post\', \'agch_properties_save_custom_meta\');
function AGCH_properties_load_wp_admin_style() {
wp_enqueue_media();
wp_enqueue_script(\'media-upload\');
wp_enqueue_style( \'AGCH_properties_admin_css\', get_template_directory_uri() . \'/library/css/properties_gallery_admin.css\' );
wp_enqueue_script( \'AGCH_properties_admin_script\', get_template_directory_uri() . \'/library/js/properties_gallery_admin.js\' );
}
add_action( \'admin_enqueue_scripts\', \'AGCH_properties_load_wp_admin_style\' );
?>
SO网友:Fab
下面是我使用acf插件在字段中保存附件的函数。这可能会有帮助
$filename = $wp_upload_dir[\'path\'].\'/\'.$img;
// Check the type of file. We\'ll use this as the \'post_mime_type\'.
$filetype = wp_check_filetype( basename( $filename ), null );
// Prepare an array of post data for the attachment.
$attachment = array(
\'guid\' => $wp_upload_dir[\'url\'] . \'/\' . basename( $filename ),
\'post_mime_type\' => $filetype[\'type\'],
\'post_title\' => preg_replace( \'/\\.[^.]+$/\', \'\', basename( $filename ) ),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . \'wp-admin/includes/image.php\' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
//Save attachment id in meta
update_field( $field, $attach_id , $post_id );
SO网友:Antti Koskinen
下面是一个简化的“伪代码”示例,您可以将图库图像设置为帖子的附件。基本思想是将当前post ID设置为image post的post\\u父级。
您应该在循环中进行某种条件检查,以检查当前迭代/图像是否应附加到帖子。否则,图像将保留在帖子上,尽管您会从帖子的元中删除它们的ID。
我希望这个示例能够说明如何设置图像发布关系,以及如何在这个帮助下修改现有代码。
function your_save_function($post_ID, $post, $update) {
// conditional checks, if this code should run or not
// loop image IDs
foreach ($gallery_image_ids as $gallery_image_id) {
// should the image be attached or removed from post
$no_parent_or_current_post = ( $some_condition ) ? $post_ID: 0;
// update attachment post parent
$att_args = array(
\'ID\' => $gallery_image_id,
\'post_parent\' => $no_parent_or_current_post,
);
$att_updated = wp_update_post( $att_args, true );
// log if something went wrong
if ( is_wp_error( $att_updated ) ) {
error_log( print_r( $att_updated, true ) );
}
}
}