Looking to display non-image files in Gallery with logo specific to file type

WordPress has a native function wp_mime_type_icon() in wp-includes/post.php that you can use.

Basic example:

// $attachment should be a full post object 

if ( wp_attachment_is_image( $attachment->ID ) )
{
    echo wp_get_attachment_image(
        $attachment->ID,
        array( 480, 900 ),
        FALSE,
        array ( 'class' => 'aligncenter' )
    );
}
else
{
    echo '<img src="' . wp_mime_type_icon( $attachment->post_mime_type ) . '">';
}

Look in wp-includes/images/crystal/ for available file type icons:

  • archive
  • audio
  • code
  • default
  • document
  • interactive
  • spreadsheet
  • text
  • video

enter image description here

You can set up your own image directory and filter 'icon_dir' for the local path and 'icon_dir_uri' for the URIs to let WordPress use your images.
To change just singular files filter 'wp_mime_type_icon':

apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id )

Leave a Comment