你需要得到metadata
在每个图像中,将此添加到functions.php
文件:
function get_post_gallery_images_with_info($postvar = NULL) {
if(!isset($postvar)){
global $post;
$postvar = $post;//if the param wasnt sent
}
$post_content = $postvar->post_content;
preg_match(\'/\\[gallery.*ids=.(.*).\\]/\', $post_content, $ids);
$images_id = explode(",", $ids[1]); //we get the list of IDs of the gallery as an Array
$image_gallery_with_info = array();
//we get the info for each ID
foreach ($images_id as $image_id) {
$attachment = get_post($image_id);
array_push($image_gallery_with_info, array(
\'alt\' => get_post_meta($attachment->ID, \'_wp_attachment_image_alt\', true),
\'caption\' => $attachment->post_excerpt,
\'description\' => $attachment->post_content,
\'href\' => get_permalink($attachment->ID),
\'src\' => $attachment->guid,
\'title\' => $attachment->post_title
)
);
}
return $image_gallery_with_info;
}
在你的逻辑中使用它,如下所示:
<?php
$gallery = get_post_gallery_images_with_info($post); //you can use it without params too
foreach( $gallery as $image_obj ) :
?>
<div class="item" style="background-image: url(\'<?php echo $image_obj[\'src\'] ?>\'); background-size: cover">
<div class="caption">
<!-- Here I want display the Title/Caption/ALT/Description of image -->
<h2><?php echo $image_obj[\'title\']." ". $image_obj[\'caption\']." ".$image_obj[\'description\']; ?> </h2>
</div>
</div>
<?php
endforeach;
?>
输出如下:
函数返回的每个图像都是如下所示的数组:
Array
(
[alt] => Alt Coffe
[caption] => Caption coffe
[description] => Description coffe
[href] => http://yoursite/2017/02/14/hello-world/coffee/
[src] => http://yoursite/wp-content/uploads/sites/4/2017/02/coffee.jpg
[title] => coffee
)
注意事项
href
和
src
是不同的,一个是permalink,另一个是direct
URL
.