Php 如何获取WP gallery图像标题?

Php 如何获取WP gallery图像标题?,php,image,gallery,wordpress,Php,Image,Gallery,Wordpress,我试图通过一个循环获取画廊图片和一篇文章的信息。我只得到图像来源,但没有标题。这是我的密码 <?php /* The loop */ while ( have_posts() ) : the_post(); if ( get_post_gallery() ) : $gallery = get_post_gallery( get_the_ID(), false ); /* Loop through all the image and outpu

我试图通过一个循环获取画廊图片和一篇文章的信息。我只得到图像来源,但没有标题。这是我的密码

<?php
/* The loop */
while ( have_posts() ) :
    the_post();
    if ( get_post_gallery() ) :
        $gallery = get_post_gallery( get_the_ID(), false );
        /* Loop through all the image and output them one by one */
        foreach( $gallery['src'] AS $src ) {
            ?>

            <img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />

            <?php
        }
    endif;
endwhile;
?>

“class=”我的自定义类“alt=”画廊图像“/>

使用这个循环,我只能在帖子中获取图库图像的源代码。但我也想获取图像标题。

而不是传递get\u ID,只需传递整个$post并使用类似的代码

$gallery = get_post_gallery( $post, false );
$gids = explode( ",", $gallery['ids'] );

foreach( $gids as $id ) {
   // here you can use the $id to fetch any details of image like below and many more  
   wp_get_attachment_url( $id );
   wp_get_attachment_metadata( $id );
} 
您可以尝试打印这些函数的值,并根据您的要求使用它

找到解决方案:

将其粘贴到functions.php中:

function wp_get_attachment( $attachment_id ) {

    $attachment = get_post( $attachment_id );
    return 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
    );
}
然后,您只需输入id并获取所需的任何元,如下所示:

attachment_meta = wp_get_attachment(your_attachment_id);
然后循环遍历数组值,或者简单地引用所需的键名(即:标题、说明等):

上面的图片将与图片的标题相呼应

这要归功于和。

对于这类事情,该函数真的很好。它返回大量关于附件的信息,我认为我们需要的所有信息

以下是被剪断的原始代码,替换为使标题可用的代码。在本例中,我已将标题放置在alt标记中:

<?php
/* The loop */
while ( have_posts() ) :
    the_post();
    if ( get_post_gallery() ) :
        $gallery = get_post_gallery( get_the_ID(), false );
        /* create an array of IDs from  */
        $gids = explode( ",", $gallery['ids'] );
        /* Loop through all the image and output them one by one */
        foreach ($gids as $id) {
            /* pull all the available attachment data with the new function */
            $attachment = wp_prepare_attachment_for_js($id);
            /* Uncomment the next line to see all the available data in $attachment */
            //var_dump($attachment); 
            /* pick and choose which bits are needed */
            ?>
            <img src="<?php echo $attachment['sizes']['thumbnail']['url']; ?>" class="my-custom-class" alt="<?php echo $attachment['caption']; ?>" />
            <?php
        }
    endif;
endwhile;
?>

“class=”我的自定义类“alt=”“/>

值得注意的是,此函数也会返回所有可用的图像大小,因此对于响应图像解决方案使用and组合时可能会非常有用:)

除了“src”之外,
$gallery
是否还包含其他字段?你能展示一下
$gallery
的整个结构吗?在我的测试中,这不会返回标题。附件的标题是我所能看到的“摘录”,而不是元数据。太好了,希望我能早点知道这个函数
<?php
/* The loop */
while ( have_posts() ) :
    the_post();
    if ( get_post_gallery() ) :
        $gallery = get_post_gallery( get_the_ID(), false );
        /* create an array of IDs from  */
        $gids = explode( ",", $gallery['ids'] );
        /* Loop through all the image and output them one by one */
        foreach ($gids as $id) {
            /* pull all the available attachment data with the new function */
            $attachment = wp_prepare_attachment_for_js($id);
            /* Uncomment the next line to see all the available data in $attachment */
            //var_dump($attachment); 
            /* pick and choose which bits are needed */
            ?>
            <img src="<?php echo $attachment['sizes']['thumbnail']['url']; ?>" class="my-custom-class" alt="<?php echo $attachment['caption']; ?>" />
            <?php
        }
    endif;
endwhile;
?>