WordPress集成FlexSlider不显示字幕

WordPress Integrated FlexSlider not showing captions

本文关键字:显示 字幕 FlexSlider 集成 WordPress      更新时间:2023-09-26

在有人将我链接到这个极其相似(如果不完全相同)的问题之前,那里提供的答案不适用于我的代码。

我正试图通过这里的功能来做每件事

然而,我似乎不知道如何添加字幕,如果且仅当所附图像有字幕,更不用说如何访问所附图像的字幕了。我有一种感觉,wp_prepare_attachment_for_js()是访问所附图片标题的方法,但我对编写函数太陌生了,甚至不知道如何在现有函数中使用它。

我当前的功能.pp:

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

但是,如果您引用的是Admin中设置的标题,则需要使用post_excerpt:

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