Thursday, January 12th, 2012 | Code, Web Dev, WordPress

Shortcode to show all files attached to post as list [showfiles]

Here’s the code:

// [showfiles]
function dump_files($atts) {
        extract(shortcode_atts(array(), $atts));
        global $post;
        $return = '';
        $children = get_children( 'numberposts=-1&orderby=menu_order&order=ASC&post_type=attachment&post_parent='.$post->ID );
        if ($children) :
        $return .= '<ul>';
        foreach ($children as $child) {
        $return .= '<li>
            <a href="'.$child->guid.'">'.$child->post_title.'</a>';
            if ($child->post_content):
                $return .='<br>'.$child->post_content;
            endif;
            $return .='</li>';
            } // endfor
        $return .='</ul>';
        endif;
        return $return;
}
add_shortcode("showfiles", "dump_files");
// end shortcode

This shortcode should go in your functions.php theme file. It is designed to pull in all post attachments, if any exist, and then print them out on the page in an unordered list. You can additionally specify mime types as a parameter in the get_children() function.

Use it in your theme by typing [showfiles] in your content.

Filed Under: Code, Web Dev, WordPress

No Comments yet

Leave a Comment