I want a simple function to display image titles and captions alongside each other below the images on wordpress posts and pages. All the answers I have seen do not work for me. Either they leave the title blank or show the post/page title instead of the image title.
This is what I have tried so far.
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 ); function my_img_caption_shortcode( $output, $attr, $content ) { $attr = shortcode_atts( array( 'id' => '', 'align' => 'alignnone', 'width' => '', 'caption' => '' ), $attr ); if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) { return ''; } if ( $attr['id'] ) { $attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" '; } return '<div ' . $attr['id'] . 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" ' . 'style="max-width: ' . ( 10 + (int) $attr['width'] ) . 'px;">' . do_shortcode( $content ) . '<p class="wp-caption-text">' . get_the_title($image_id) . ", " . $attr['caption'] . '</p>' . '</div>'; }