WordPress Integrated FlexSlider not showing captions

Before anyone links me to this EXTREMELY similar (if not identical) question, the answer provided there does not work with my code.

I’m attempting to do everything via a function as found here

Read More

However, I can’t seem to figure out how to add captions if and ONLY if the attached image has one let alone how to access the attached image’s caption.
I have a feeling wp_prepare_attachment_for_js() is the way to go in accessing the attached image’s caption, but I’m so new at writing functions I don’t even know how to utilize it within my existing function.

My current 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>';

    } 

} 

Related posts

1 comment

  1. There are a lot of ways to do this…wp_get_attachment_metadata() is one of them:

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

    However, if you’re referring to the caption set in the Admin, you’ll want to use the post_excerpt:

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

Comments are closed.