Skip to content

Instantly share code, notes, and snippets.

@gyrus
Created July 21, 2012 21:04
Show Gist options
  • Select an option

  • Save gyrus/3157179 to your computer and use it in GitHub Desktop.

Select an option

Save gyrus/3157179 to your computer and use it in GitHub Desktop.
Simple list of download files attached to WordPress post or page
<?php
/**
* List of downloads
*
* @uses get_children()
* @uses esc_url()
* @uses apply_filters()
* @uses get_attached_file()
* @uses pilau_format_filesize() (find on Gist)
*/
function pilau_downloads_list( $post_id = null ) {
if ( is_null( $post_id ) ) {
global $post;
$post_id = $post->ID;
}
$downloads = get_children( 'post_parent=' . $post_id . '&post_type=attachment&post_mime_type=application' );
if ( count( $downloads ) ) {
echo '<div class="downloads">';
echo '<h3>' . __( "Downloads" ) . '</h3>';
echo '<ul>';
foreach ( $downloads as $download ) {
echo '<li><a href="' . esc_url( $download->guid ) . '">' . apply_filters( 'the_title', $download->post_title ) . '</a> (' . strtoupper( end( explode( ".", $download->guid ) ) );
if ( function_exists( 'pilau_format_filesize' ) )
echo ', ' . pilau_format_filesize( get_attached_file( $download->ID ) );
echo ')</li>';
}
echo '</ul>';
echo '</div>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment