WP如何获取到上传附件或者图片的所有信息

<?php
$attachment_id = 123; // Replace with your ID
$attachment = get_post($attachment_id);

if ($attachment) {
    // 1. Basic Information
    $title       = $attachment->post_title;
    $caption     = $attachment->post_excerpt; // WordPress uses excerpt for caption
    $description = $attachment->post_content; // WordPress uses content for description
    $mime_type   = get_post_mime_type($attachment_id);
    
    // 2. URLs and Paths
    $file_url    = wp_get_attachment_url($attachment_id);
    $file_path   = get_attached_file($attachment_id);
    
    // 3. Metadata
    $alt_text    = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
    
    // 4. File Size (Human Readable)
    $file_size   = size_format(filesize($file_path));

    // 5. Technical Metadata (Width, Height, etc. for images)
    $meta_data   = wp_get_attachment_metadata($attachment_id);
    $width       = $meta_data['width'] ?? 'N/A';
    $height      = $meta_data['height'] ?? 'N/A';

    // --- Outputting the Data ---
    echo "<h3>Attachment Info</h3>";
    echo "<ul>";
    echo "<li><strong>Title:</strong> {$title}</li>";
    echo "<li><strong>Alt Text:</strong> {$alt_text}</li>";
    echo "<li><strong>Caption:</strong> {$caption}</li>";
    echo "<li><strong>Description:</strong> {$description}</li>";
    echo "<li><strong>URL:</strong> <a href='{$file_url}'>View File</a></li>";
    echo "<li><strong>Mime Type:</strong> {$mime_type}</li>";
    echo "<li><strong>File Size:</strong> {$file_size}</li>";
    
    if ($width !== 'N/A') {
        echo "<li><strong>Dimensions:</strong> {$width} x {$height} px</li>";
    }
    echo "</ul>";
} else {
    echo "No attachment found with that ID.";
}
?>
×
WP如何获取到上传附件或者图片的所有信息

WP如何获取到上传附件或者图片的所有信息

<?php $attachment...