Here’s one way, out of many, where we use the the_content
filter and loop through the get_attached_media( $type )
to construct the list of attached media. We can e.g. use $type="image"
for images or $type = null
for all mime types.
add_filter( 'the_content', function( $content )
{
// Nothing to do - target single posts assigned to the 'report' category
if( ! in_the_loop() || ! is_single() || ! has_category( 'report' ) )
return $content;
// Reduce attached media items into a string
$li = array_reduce(
get_attached_media( $type = null ), // Fetch the attached media (all == null)
function( $li, $item ) // Callback for each media item
{
$li .= sprintf( // Construct and append each list item
'<li>%s</li>',
wp_get_attachment_link( $item->ID, false )
);
return $li;
},
'' // Initial value of $li
);
// Append to the content
if( $li )
$content .= sprintf( '<ul class="attached-media">%s</ul>', $li);
return $content;
}, 999 ); // Some late priority
Here we display the attached media list only for single posts, assigned to the report
category.