如何获取图库图像的图像描述/标题/Alt?

时间:2017-02-16 作者:Zkk

我正在显示库图像,但我还想显示图像的标题。我可以在WordPress仪表板中上传图像时获得我们插入的信息,如“标题/标题/替换/描述”。我想找人来展示一下。

<?php    
    $gallery = get_post_gallery_images( $post );
    foreach( $gallery as $image_url ) :    
?>                                

    <div class="item" style="background-image: url(\'<?php echo $image_url ?>\'); background-size: cover">
        <div class="caption">                
            <!-- Here I want display the Title/Caption/ALT/Description of image -->
            <h2><?php echo $image_url->"DESCRIPTION/TITLE/ALT"; ?> </h2>
        </div>                                        
    </div>
阅读的文档get_post_gallery_images 我没有找到解决问题的方法<我还发现this answer 但我不知道它是否有效,我的代码中还需要实现错误。

不管怎样,我怎样才能解决这个问题?

2 个回复
最合适的回答,由SO网友:David Lee 整理而成

你需要得到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;
?>
输出如下:

enter image description here

函数返回的每个图像都是如下所示的数组:

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
        )
注意事项hrefsrc 是不同的,一个是permalink,另一个是directURL.

SO网友:Kyon147

图像的标题实际上是附加到图像的meta\\u数据get_post_gallery_images only returns a url, 因此,在数组中,您不会有任何其他信息。

您可以尝试以下方式:

<?php    
    $gallery = get_post_gallery_images( $post );
    foreach( $gallery as $image_url ) :  

    //get the id of the image post.
    $image_id = url_to_postid( $image_url ) 
    //get the image "post" information
    $image = get_post($image_id);
    //get the image title
    $image_title = $image->post_title;
    //get the image caption
    $image_caption = $image->post_excerpt;

?>                                

    <div class="item" style="background-image: url(\'<?php echo $image_url ?>\'); background-size: cover">
        <div class="caption">                
            <!-- Here I want display the Title/Caption/ALT/Description of image -->
            <h2><?php echo $image_caption; ?> </h2>
        </div>                                        
    </div>