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

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 )
Related Posts:
- Personalized Gallery what are my options?
- Modified wp.media.view.Settings.Gallery in Backbone JS, but editing doesn’t work
- Rename UPLOADS folder with custom WP_CONTENT_DIR
- How to move wp-content (or uploads) outside of the WordPress directory
- Limit Media Library to Given Folder
- Customizing gallery shortcode in 3.5
- Gallery Shortcode: using link attribute to link to a specific image size
- Automatically wrap post image in div
- media_handle_upload weird thing
- Syncing Avatars across multisite
- How to re-arrange media uploaded using : media_handle_upload() as per year / month
- Hide upload image fields for the different media upload popups
- Customize the WordPress Default Gallery Output
- Best way to implement a thumbnail-based gallery in WordPress?
- How to insert a sitemap within a subsite when using WordPress multisite
- Gallery IDS and Captions: hide/show on a per ID basis
- Modify Gallery Output to render differently on a PAGE and a POST
- Add a InfoBox in the media upload window
- I’m using add_meta_box() but want to include TinyMCE editor *and* Media Uploads for each box
- Get ORIGINAL image links from get_post_gallery
- WordPress media uploader – upload tabs not hiding
- custom upload directory only for plugin uploads
- problem in uploading attachment to custom directory
- Installing new theme on running site
- Media Upload not working after hiding some elements from functions.php
- Customizing the gallery of the post correctly
- Custom default settings for WP native Gallery Block
- Can’t upload format files on media library
- Lightbox not showing image caption
- the temporary save folder is wrong
- Altered Media Library URLs
- How do I enable the text editer in my WordPress theme? [closed]
- Is there a way to limit multi upload in media upload box?
- Custom Link In WordPress Media “Attachment Details” Upload Screen
- How to customize the gallery shortcode in WordPress?
- How can I add a Description column to the media library browser screen?
- Upload files in wordpress
- Custom image sizes ignored in WP 3.5
- WordPress uploads directory. Featured Images storage
- Gallery Pagination by Row
- How to disable the PDF preview in the file block by default
- Multiple header style under meta box
- Use a custom page with dynamic content as a user profile page
- website is not loading home page (moving from one server to another server)
- Best practice to generate token for email action
- Style WooCommerce Product Search Widget
- Display all subterms of a custom taxonomy filtered by parent terms
- how to change the url rediction of the woocommerce login page of the flatsome theme for my own in wordpress?
- My list of terms of custom taxonomy displays 7 times
- why won’t my custom css load with the enqueue method or any other method?
- Delete a column in edit.php generated with a plugin
- Customizer options limited to specific user roles?
- Product page not found if product data is set Appointment Service
- Block direct access to thank you pages
- How do I add a dropdown menu to a form?
- What is my error trying to create a new table in the database?
- I want to embed mystream video in my wordpress site
- Current menu item highlights wrong
- Adding a custom field to a slug
- Measure time in ONLY HOUR format
- Add admin page for specific post status (for example)
- Query Product categories what have a custom field
- Get /users/me details from WordPress backend
- mailchimp integration on a custom footer
- Can’t Customize WordPress Page
- ACF unable to use user field
- Bootstrap slider not working
- Use Theme CSS in custom code
- How to get the full product name by ignoring custom modification on it
- adding fade on mouseover to images
- where is admin footer?
- Customize section does not show my widget areas
- How can i add customisation of text to my theme?
- Add producthunt badge to wordpress menu
- Can I overwrite custom taxonomy FROM “public” TO “false”
- Show products per page drop down
- Trying to customise 2011 Child Theme Background Colour
- How to remove (…) from the post tilte on homepage?
- Is it possible to edit a wordpress category with Gutenberg?
- Widget recent comment filter by post meta_value
- Invitation link to a site in multisite network does not work?
- Swap home page based on user role, Elementor layout breaking
- Add helper text in custom-header section in admin
- How to edit content in code view of home page when there is some theme applied?
- How to display strikethrough text in button text (for special offer)
- Adding shortcode to product short description
- Display Custom Taxonomy Name As A Shortcode
- How to change Login default blue admin color?
- Remove Title and White Space in Twenty Twenty theme
- How do i remove this from logged in users?
- Schedule a subscription on gravity form, stripe add-on
- How to show related post with category DESC (ASC) IDs of ID current post
- Lost customization after switching the WordPress theme
- How to show Term ID beside Name Category with wp_dropdown_categories()
- Gender based user avatar
- Migrating my site to a new domain does not keep any changes made in customizer
- Woocommerce custom “My Account” Tab – Add Save button
- How to create custom backend admin menu in different languages?
- WordPress Using Zip code customer shop find without woocommerce plugin
- Is MultiSite the Correct Solution for a Corporate Site with Searchable Regions?