I am trying to get the caption of post embedded images to then use on a lightbox.
This is my code:
function my_theme_prefix_content_gallery( $content ) {
global $post;
$args = array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID,
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
}
}
$pattern = "/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$replacement = '<a$1href=$2$3.$4$5 rel="lightbox" title="' . $caption . '"$6>';
$content = preg_replace( $pattern, $replacement, $content );
return $content;
}
add_filter( 'the_content', 'my_theme_prefix_content_gallery' );
What i have at the moment(code above) is giving me the caption from other images on the media library. How do I get the caption of the image that I am seeing?
EDIT
The Magnific Popup bit for the embedded images popup:
$('a[rel="lightbox"]').magnificPopup ({
type: 'image',
mainClass: 'mfp-fade',
fixedContentPos: false,
removalDelay: 350,
gallery: {
enabled: true
}
});
So whit this code I am being able to open post embedded images in Magnific Popup when clicked. All I am missing is a way to show their captions when they open in Magnific Popup.