我有一个帖子格式库。我已经创建了一个元盒来添加我的图像:
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 );
?>
<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 ); ?>
<li>
<input type="hidden" name="_mytheme_post_gallery[<?php echo $key; ?>]" value="<?php echo $value; ?>">
<img class="image-preview" src="<?php echo $image[0]; ?>">
<a class="change-image" href="#"><?php _e( \'Change | \', \'mytheme\' ); ?></a>
<a class="remove-image" href="#"><?php _e( \'Remove\', \'mytheme\' ); ?></a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</td>
</tr>
</table>
<?php
}
此代码允许我通过metabox将图像添加到我的库中。我现在也在尝试获取每个图像的标题。我已经尝试了很多方法,但到目前为止运气不好。。任何想法都将不胜感激。
最合适的回答,由SO网友:jgraup 整理而成
获取附件ID并转换为帖子。从那里,标题存储在post对象上。
$thumb_img = get_post( get_post_thumbnail_id() ); // Get post by ID
echo $thumb_img->post_excerpt; // Display Caption
echo $thumb_img->post_content; // Display Description
在您的循环中,它看起来像:
<?php if($gallery) : ?>
<?php foreach($gallery as $key => $value) :
$image = wp_get_attachment_image_src($value);
$image_post = get_post($value);
$caption = $image_post->post_excerpt;
您希望如何输出值取决于您和您的css期望;
<p class="wp-caption-text"><?php echo $caption; ?></p>
另一种选择是
Caption_Shortcode:
[caption]<image> Caption[/caption]