The below shortcode, [documents]
, when installed as a plugin, will display images with links and captions from the Description field for the current page:
<?php
/**
* Plugin Name: Documents Shortcode
* Description: Display images with links and captions from the <code>documents</code> field.
* Author: ??δ???
* Author URI: https://pd.cm/
*/
add_action(
'plugins_loaded',
function(){
add_shortcode(
'documents',
function( $atts ) {
ob_start();
$document_ids = get_post_meta( get_the_ID(), 'documents', false );
foreach( $document_ids as $document_id ) {
printf(
'<figure>
<a href="%s">
<img src="%s" alt="%s" />
<figcaption>%s</figcaption>
</a>
</figure>',
wp_get_attachment_image_src( $document_id, 'full' )[0],
wp_get_attachment_image_src( $document_id, 'thumbnail' )[0],
esc_attr( get_post( $document_id )->post_content ),
esc_html( get_post( $document_id )->post_content )
);
}
return ob_get_clean();
}
);
}
);