WordPress Integrated FlexSlider不显示字幕

时间:2015-12-18 21:30:02

标签: javascript php jquery wordpress flexslider

在有人将我链接到此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>';

    } 

} 

1 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点...... 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;