【问题标题】:How to get WP gallery Image Captions?如何获取 WP 图库图片标题?
【发布时间】:2014-11-16 10:13:27
【问题描述】:

我正在尝试通过循环抓取图库图片,它是帖子的信息。我得到的只是图像来源,而不是标题。这是我的代码

<?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;
?>

使用此循环,我只能在帖子中获取画廊图像的来源。但我也想抓取图片说明。

【问题讨论】:

  • $gallery 是否包含“src”以外的其他字段?你能展示$gallery的整个结构吗?

标签: php image gallery wordpress


【解决方案1】:

wp_prepare_attachment_for_js 函数非常适合这类事情。它返回大量有关附件的信息,我认为我们将需要的所有信息。

这里的原始代码被剪断,替换为使标题可用的代码。在这种情况下,我将标题放在 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;
?>

值得注意的是,此函数还返回所有可用的图像尺寸,因此在使用 custom image sizessrcset 的组合来实现响应式图像解决方案时可能会很棒:)

【讨论】:

  • 太好了,希望我早点知道这个功能
【解决方案2】:

找到了解决办法on wordpress.org

把它贴在你的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);

然后要么循环遍历数组值,要么简单地通过你想要的键名引用(即:标题、描述等):

echo $attachment_meta['caption'];

以上内容会呼应图片的标题。

为此归功于 Luke Mlsnasporkme

【讨论】:

    【解决方案3】:

    而不是传递 get_the_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 );
    } 
    

    您可以尝试打印这些函数的值并根据您的要求使用它

    【讨论】:

    • 在我的测试中,这不会返回标题。据我所知,附件的标题是“摘录”,而不是元数据。
    猜你喜欢
    • 2012-10-11
    • 1970-01-01
    • 2020-04-20
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多