WordPress Integrated FlexSlider 不显示字幕

WordPress Integrated FlexSlider not showing captions

在任何人将我链接到此 EXTREMELY similar (if not identical) question 之前,那里提供的答案不适用于我的代码。

我正在尝试通过发现的函数来完成所有事情 here

但是,当且仅当附加图像有一个时,我似乎无法弄清楚如何添加标题,更不用说如何访问附加图像的标题了。 我感觉 wp_prepare_attachment_for_js() 是访问附加图像标题的方式,但我在编写函数方面太新了,我什至不知道如何在我现有的函数中使用它。

我现在的 functions.php:

//Add Flexslider
function add_flexslider() { 

    global $post; 

    $attachments = get_children ( array(
        'post_parent' => $post->ID, 
        'order' => 'ASC', 
        'orderby' => 'menu_order', 
        'post_type' => 'attachment', 
        'post_mime_type' => 'image', 
        ));

    if ($attachments) { 

        echo '<div class="flexslider">';
        echo '<ul class="slides">';

        foreach ( $attachments as $attachment_id => $attachment ) { 

            echo '<li>';
            echo wp_get_attachment_image($attachment_id, 'large');
            //if statement that shows the caption only if attached image has one
            echo '<p class="flex-caption">';
            //somehow get attached image's caption. perhaps with wp_prepare_attatchment_for_js()?
            echo '</p>';
            //end if caption statement
            echo '</li>';

        }

        echo '</ul>';
        echo '</div>';

    } 

} 

有很多方法可以做到这一点...wp_get_attachment_metadata() 是其中之一:

$metadata = wp_get_attachment_metadata( $attachment_id );
$caption = $metadata ? $metadata['image_meta']['caption'] : '';

echo $caption;

但是,如果您指的是在管理员中设置的标题,则需要使用 post_excerpt:

$attachment = get_post( $attachment_id );
$caption = $attachment->post_excerpt;

echo $caption;