0

I've tried various things and can't get the captions to show up in my gallery. I'm using CMB2's file list to output image attachments. I hope this question is pertinent enough to this forum as I think I'm just missing some simple php or wordpress function. Currently, it does output the image and the empty p tags. Thanks for any help!

In the past I've used echo get_post(get_post_thumbnail_id())->post_excerpt; in the post template.

function soth_output_gallery_file_list( $file_list_meta_key, $img_size ) {

// Get the list of files
$files = get_post_meta( get_the_ID(), $file_list_meta_key, 1 );

echo '<div class="entry-content row">';

  foreach ( (array) $files as $attachment_id => $attachment_url ) {
    echo '<div class="small-6 medium-4 column text-center">';
    echo wp_get_attachment_image( $attachment_id, $img_size );
    echo '<p class="wp-caption-text">';
//what am I missing?
    echo $missingSomething->post_excerpt;
    echo '</p></div>';
  }

echo '</div>';
}
  • Why not using the same line again echo get_post(get_post_thumbnail_id())->post_excerpt; ? – Sumit May 27 '16 at 6:22
  • I don't know why, but I can't get that to work. I've also tried get_post_meta(get_the_ID())->post_excerpt;... That's what makes me think I'm writing it with the wrong WP function or php syntax... – BradH May 27 '16 at 23:41
1

You simply need to pass $attachment_id instead of get_post_thumbnail_id() in echo get_post(get_post_thumbnail_id())->post_excerpt; with wptexturize to prevent your markup from breaking other things.

Update: As @birgire suggested I think it is better to use get_post_field() instead of directly accessing post property.

Updated code:-

function soth_output_gallery_file_list( $file_list_meta_key, $img_size ) {

    // Get the list of files
    $files = get_post_meta( get_the_ID(), $file_list_meta_key, 1 );

    echo '<div class="entry-content row">';

        foreach ( (array) $files as $attachment_id => $attachment_url ) {
            echo '<div class="small-6 medium-4 column text-center">';
            echo wp_get_attachment_image( $attachment_id, $img_size );
            echo '<p class="wp-caption-text">';

            //Get post excerpt by attachment ID
            echo wptexturize( get_post_field( 'post_excerpt', $attachment_id ) );
            echo '</p></div>';
        }

    echo '</div>';
}
  • 1
    Yes! That works. Thanks very much. That's what I was missing, I didn't know how to get that object and that I needed to use it as a variable. I've got a lot to learn still and appreciate your help. – BradH May 28 '16 at 18:23
  • 1
    Another option could be through get_post_field() and note that wptexturize() will not escape possible HTML code in the excerpt or fix problematic HTML code. What about esc_html() ? @BradH Please accept the answer if it solved the problem, to close the question, thanks. – birgire May 28 '16 at 19:31
  • 1
    @birgire Thanks! I think it is better to use get_post_field to allow filters and saving few lines (isset() etc). But I am not sure about esc_html() here. I used wptexturize() because default gallery shortcode uses it. For me I never put HTML in excerpt :D – Sumit May 28 '16 at 20:24
  • Another question if I may (or do I need to ask a new one??)... In order to get an image to open in my lightbox, I need it to link to the attachment. I've added the variable $link = wp_get_attachment_url( $attachment_obj ); and echo '<a href="'. $link .'">'; and it just goes to the post page that the gallery is on. But that's the only think remotely close to working I've tried... – BradH May 28 '16 at 22:37
  • @BradH That should be an another question :) but you can try wp_get_attachment_image_src() instead of wp_get_attachment_url() if it works. And do not forgot to check codex examples – Sumit May 29 '16 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.