admin管理员组

文章数量:1123931

I have page and post in which user upload files as attachment and display link for donwload this files. How I can display after the size link type of uploaded file and the size?

After answer in this therad I have the code

$attachments = get_posts( array(
                            'post_type' => 'attachment',
                            'posts_per_page' => -1,
                            'post_parent' => $post->ID,
                            'exclude'     => get_post_thumbnail_id()
                        ) );

                if ( $attachments ) {
                    foreach ( $attachments as $attachment ) {
                        // Assuming $attachment_id contains the ID of the attachment
                        $attachment_metadata = wp_get_attachment_metadata($attachment->ID);

                        if ($attachment_metadata) {
                            // Get file size in bytes
                            $file_size = filesize(get_attached_file($attachment->ID));

                            // Convert file size to human-readable format
                            $file_size_human = size_format($file_size);

                            // Get file type
                            $file_type = $attachment_metadata['mime_type'];
                            //$file_type = get_post_mime_type($attachment->ID);
                            $file_url = wp_get_attachment_url( $attachment->ID);
                            $filetype = wp_check_filetype( $file_url );

                            // Display file size and type
                            //echo '<p>File Size: ' . $file_size_human . '</p>';
                            //echo '<p>File Type: ' . $filetype['ext'] . '</p>';
                            $link = wp_get_attachment_link($attachment->ID);
                            echo '<li>' . $link . '( ' . $file_size_human . ', ' . $filetype['ext'] .')</li>';
                        }

code works for me, but another problem is that attachment is display on function the_content(). How is the way to modify the_content to use only my own function for display links to attachment?

I have page and post in which user upload files as attachment and display link for donwload this files. How I can display after the size link type of uploaded file and the size?

After answer in this therad I have the code

$attachments = get_posts( array(
                            'post_type' => 'attachment',
                            'posts_per_page' => -1,
                            'post_parent' => $post->ID,
                            'exclude'     => get_post_thumbnail_id()
                        ) );

                if ( $attachments ) {
                    foreach ( $attachments as $attachment ) {
                        // Assuming $attachment_id contains the ID of the attachment
                        $attachment_metadata = wp_get_attachment_metadata($attachment->ID);

                        if ($attachment_metadata) {
                            // Get file size in bytes
                            $file_size = filesize(get_attached_file($attachment->ID));

                            // Convert file size to human-readable format
                            $file_size_human = size_format($file_size);

                            // Get file type
                            $file_type = $attachment_metadata['mime_type'];
                            //$file_type = get_post_mime_type($attachment->ID);
                            $file_url = wp_get_attachment_url( $attachment->ID);
                            $filetype = wp_check_filetype( $file_url );

                            // Display file size and type
                            //echo '<p>File Size: ' . $file_size_human . '</p>';
                            //echo '<p>File Type: ' . $filetype['ext'] . '</p>';
                            $link = wp_get_attachment_link($attachment->ID);
                            echo '<li>' . $link . '( ' . $file_size_human . ', ' . $filetype['ext'] .')</li>';
                        }

code works for me, but another problem is that attachment is display on function the_content(). How is the way to modify the_content to use only my own function for display links to attachment?

Share Improve this question edited Mar 21, 2024 at 8:40 mardon asked Mar 20, 2024 at 9:22 mardonmardon 1216 bronze badges 1
  • a note that this info is stored in the DB, not read from the file, so if you adjust the file manually via FTP it won't change when displayed on the frontend. I also don't see any code in your question for what you've done so far to display the attachments – Tom J Nowell Commented Mar 20, 2024 at 11:15
Add a comment  | 

1 Answer 1

Reset to default 1

you can use within your page or post template and also replace $attachment_id with the ID of the attachment you want to display information for.

// Assuming $attachment_id contains the ID of the attachment
$attachment_metadata = wp_get_attachment_metadata($attachment_id);

if ($attachment_metadata) {
    // Get file size in bytes
    $file_size = filesize(get_attached_file($attachment_id));
    
    // Convert file size to human-readable format
    $file_size_human = size_format($file_size);
    
    // Get file type
    $file_type = $attachment_metadata['mime_type'];
    
    // Display file size and type
    echo '<p>File Size: ' . $file_size_human . '</p>';
    echo '<p>File Type: ' . $file_type . '</p>';
}

// Display attachment link
echo wp_get_attachment_link($attachment_id);

本文标签: Display file size and type on page or post with attachments