我不得不生成一堆额外的代码,因为我不认为您已经发布了所有代码。在这种情况下,我没有脚本来添加库,所以我通过在帖子中提取库数据来填充元框来测试它。每个标题文本字段是根据图像生成的,然后在保存后更新。
这将显示元框和库中的每个图像,每个图像带有标题和两个按钮。更改标题文本将在保存时设置图像的post\\u摘录。
function mytheme_show_post_gallery_metabox($post) {
wp_nonce_field(\'mytheme_post_gallery_metabox\', \'_mytheme_post_gallery_metabox\');
$gallery = get_post_meta($post->ID, \'_mytheme_post_gallery\', true);
// START | TESTING
// pull the gallery images from all galleries if it hasn\'t been created
if(empty($gallery)) {
$galleries = get_post_galleries($post, false);
$allIds = array();
foreach($galleries as $data) {
$ids = $data[ \'ids\' ];
$ids = explode(\',\', $ids);
$allIds = array_unique ( array_merge( $allIds, $ids ) );
}
$gallery = $allIds;
}
// END | TESTING ?>
<style>
ul#gallery-metabox-list li {
float: left;
margin-right: 5px;
}
</style>
<a class="gallery-add button media-button button-primary button-large media-button-select" href="#"
data-uploader-title="<?php _e(\'Add gallery images\', \'mytheme\'); ?>"
data-uploader-button-text="<?php _e(\'Add gallery images\', \'mytheme\'); ?>"><?php _e(\'Add gallery images\', \'mytheme\'); ?></a>
<table>
<tr>
<td>
<ul id="gallery-metabox-list">
<?php if($gallery) : ?>
<?php foreach($gallery as $key => $value) :
$image = wp_get_attachment_image_src($value);
$image_id = $value;
$image_post = get_post($image_id);
$caption = $image_post->post_excerpt;
?>
<li>
<input type="hidden" name="_mytheme_post_gallery[<?php echo $key; ?>]"
value="<?php echo $value; ?>">
<img class="image-preview" src="<?php echo $image[ 0 ]; ?>">
<p>
<label for="_mytheme_caption_text_<?php echo $image_id; ?>"
class="mytheme-row-title"><?php _e(\'Caption\', \'mytheme\') ?></label>
<input type="text" name="_mytheme_caption_text_<?php echo $image_id; ?>"
id="_mytheme_caption_text_<?php echo $image_id; ?>"
value="<?php echo $caption; ?>"/>
</p>
<p>
<a class="change-image" href="#"><?php _e(\'Change\', \'mytheme\'); ?></a> | <a
class="remove-image" href="#"><?php _e(\'Remove\', \'mytheme\'); ?></a>
</p>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</td>
</tr>
</table>
<?php
}
设置元框
add_action(\'add_meta_boxes\', \'myplugin_add_meta_box\');
function myplugin_add_meta_box() {
$screens = array(\'post\', \'page\');
foreach($screens as $screen) {
add_meta_box(
\'myplugin_sectionid\',
__(\'My Post Section Title\', \'mytheme\'),
\'mytheme_show_post_gallery_metabox\', // meta box view
$screen
);
}
}
保存时,更新库并提取ID。对于每个ID,检查并查看post数据中是否有标题文本字段。如果存在,则更新图像的帖子内容。
add_action(\'save_post\', \'mytheme_save_image_gallery_metabox\');
function mytheme_save_image_gallery_metabox($post_id) {
// see: http://themefoundation.com/wordpress-meta-boxes-guide/
if( ! isset($_POST[ \'_mytheme_post_gallery\' ]) || ! wp_verify_nonce($_POST[ \'_mytheme_post_gallery_metabox\' ], \'mytheme_post_gallery_metabox\')) return;
if( ! current_user_can(\'edit_post\', $post_id)) return;
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) return;
if(isset($_POST[ \'_mytheme_post_gallery\' ])) {
$gallery = wp_kses($_POST[ \'_mytheme_post_gallery\' ], \'\');
update_post_meta($post_id, \'_mytheme_post_gallery\', $gallery);
foreach($gallery as $image_id) {
if( ! is_numeric($image_id)) continue; // not a number, not an ID
if(isset($_POST[ \'_mytheme_caption_text_\' . $image_id ])) {
$caption = $_POST[ \'_mytheme_caption_text_\' . $image_id ];
$caption = wp_kses($caption); // clean it up
// change the image caption
wp_update_post(array(\'ID\' => $image_id, \'post_excerpt\' => $caption));
}
}
}
else {
delete_post_meta($post_id, \'_mytheme_post_gallery\');
}
}