admin管理员组

文章数量:1122846

When using insert media into a page it shows a grid of pictures. How can I change that to see file names instead (or both)? And would there be a way to have the images sort by file name?

When using insert media into a page it shows a grid of pictures. How can I change that to see file names instead (or both)? And would there be a way to have the images sort by file name?

Share Improve this question asked May 10, 2024 at 21:25 CAlexanderCAlexander 1
Add a comment  | 

1 Answer 1

Reset to default 1

By default, WordPress doesn't provide a built-in option to sort media attachments by file name in the media library interface. However, you can achieve this programmatically or with the help of plugins.

  • Using Plugins: Plugins like "Media Library Assistant" or "Media Library Categories" offer advanced sorting and filtering options for the media library, including sorting by file name.

  • Custom Code: If you prefer custom development, you can use WordPress hooks and filters to modify the query parameters for retrieving media attachments and sort them by file name.

Here's a basic example:

    function custom_media_library_sort_by_filename($query) {
    if (is_admin() && $query->is_main_query() && $query->get('post_type') == 'attachment') {
        $query->set('orderby', 'title');
        $query->set('order', 'ASC'); // or 'DESC' for descending order
    }
}
add_action('pre_get_posts', 'custom_media_library_sort_by_filename');

This code snippet hooks into the pre_get_posts action to modify the query parameters for retrieving media attachments. It sets the orderby parameter to 'title' (which represents the file name) and specifies the sorting order.

Whether you choose to use a plugin or custom code depends on your preferences and the specific requirements of your website. Plugins offer convenience and often come with additional features, while custom code provides more flexibility and control over the implementation.

本文标签: When inserting media file in a post or pageshow file name under the thumbnail