【问题标题】:Get image caption in Wordpress在 Wordpress 中获取图片标题
【发布时间】:2014-06-03 00:59:12
【问题描述】:

我有这段代码可以检索画廊中图片的图片网址,它工作正常,但我不知道如何获取每张图片的标题。

我尝试搜索所有信息,但我似乎无法将所有信息放在一起!关于如何检索字幕的任何建议?

 function show_related_gallery_image_urls( $content ) {

    global $post;

    // Only do this on singular items
    if( ! is_singular() )
        return $content;

    // Make sure the post has a gallery in it
    if( ! has_shortcode( $post->post_content, 'gallery' ) )
        return $content;



    // Retrieve all galleries of this post
    $galleries = get_post_galleries_images( $post );

    $image_list = <<<END
                    <div class="side_bar">
            <div class="related">
                <h3>Related Images</h3>
END;

    // Loop through all galleries found
    foreach( $galleries as $gallery ) {

        // Loop through each image in each gallery
        foreach( $gallery as $image ) {

            $src = $image;

            $image_list .=  '<a href="' . $src . '" rel="' . get_the_title() . '">' 
                                . '<img src="' . $src .'" />' 
                            . '</a>';

        }

    }

    $image_list .= '</div></div>';

    // Append our image list to the content of our post
    $content .= $image_list;

    return $content;

 }
 add_filter( 'the_content', 'show_related_gallery_image_urls' );

我希望我能很好地解释自己!谢谢!

【问题讨论】:

标签: php wordpress


【解决方案1】:

这还没有通过尝试测试,我清理了你的一些代码:

1) 将前2个IF语句合并为1个

2) 使用了get_post_gallery() (Codex),它返回src 和图像ID。我们使用图像 ID 来返回标题,如果需要,我们还可以获取描述等。

3) 删除了包含 Foreach 语句,因为我的方法都只返回 1 个画廊,而不是多个画廊,因此无需循环。

function show_related_gallery_image_urls( $content ) {
    global $post;

    // Only do this on singular items 
    if( ! is_singular() || !has_shortcode( $post->post_content, 'gallery' ) )
        return $content;

    // Retrieve all galleries of this post
    $galleries = get_post_gallery( $post, false );

    $image_list = <<<END
                    <div class="side_bar">
            <div class="related">
                <h3>Related Images</h3>
END;

    // Loop through each image in each gallery
    $i = 0; // Iterator
    foreach( $gallery['src'] as $src ) {
        $caption = wp_get_attachment($gallery['id'][$i])['caption'];

        $image_list .=  '<a href="' . $src . '" rel="' . get_the_title() . '">' 
                        . '<img src="' . $src .'" />'
                        . '<div class="caption">'
                            . $caption
                        . '</div>'
                    . '</a>';

        $i++; // Incremenet Interator
    }

    $image_list .= '</div></div>';

    // Append our image list to the content of our post
    $content .= $image_list;

    return $content;

 }
 add_filter( 'the_content', 'show_related_gallery_image_urls' );

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
    );
}

在旁注中,有一个图库过滤器功能,您可以在其中更改图库的显示方式post_gallery Filterhere's a question 那种显示如何编辑它。还有一个很棒的WordPress Stack Exchange,将来可能会有所帮助!

【讨论】:

  • 代码有效(除了 $caption 后的
    后缺少引号),但它仍然不会返回标题...还有其他建议吗?谢谢!!
  • echo print_r(get_post(get_post_thumbnail_id($gallery['id'][$i]))); 看到了什么?
  • 'WP_Post 对象([ID] => 179 [post_author] => 1 [post_date] => 2014-04-18 13:49:38 [post_date_gmt] => 2014-04-18 13 :49:38 [post_content] => [post_title] => _MG_7936 [post_excerpt] => [post_status] => 继承 [comment_status] => 打开 [ping_status] => 打开 [post_password] => [post_name] => _mg_7936 [ to_ping] => [pinged] => [post_modified] => 2014-04-18 13:49:38 [post_modified_gmt] => 2014-04-18 13:49:38 [post_content_filtered] => [post_parent] => 105 [guid] => creativewp/wp-content/uploads/2014/04/MG_7936.jpg [menu_order] ...
  • 我为画廊中的每张图片都得到了这个
  • 你得到相同的输出,还是不同的输出得到相同的结果? post_contnet 将是描述字段,摘录将是标题。确保每张图片都有标题以验证其是否有效。
  • 猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    相关资源
    最近更新 更多