I'm writing a site with a category that's used for formatting specific types of reports.

The user logs in, and writes the report as a blog entry. When they select the category, the report is formatted in a certain way.

I'm editing the "single.php" file in my child theme (I'm deriving from WooCommerce mystile)

What I want to do is have a runtime (display time) feature that finds attachments that were made JUST TO THIS POST, and create a list of links to those attachments at the bottom of the post.

The Hooks Page doesn't seem to have anything, but I'm sure there's something.

Does anyone know the proper "M" for me to RTFM?



1 Answer 1


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
                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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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