You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何在WordPress/PHP中显示媒体的上传日期?

How to Display Media Upload Dates in WordPress

Hey there! Sorry to hear you’ve had no luck finding a working solution on StackOverflow so far—let’s fix that. Media files in WordPress are actually stored as "attachment" post types, so we can leverage core WordPress functions to pull their upload date reliably. Here are a few tested methods you can drop into your page template:

Method 1: Use get_post_time() (Simplest Approach)

Since attachments are posts, get_post_time() will grab the upload date directly from the attachment's post data. You just need to pass the attachment ID (if you’re outside the loop) or let it use the current post ID (if you’re inside an attachment loop).

Example code for a single attachment:

// Replace $attachment_id with your actual media ID
$attachment_id = 123;
$upload_date = get_post_time('F j, Y', true, $attachment_id);
echo '<p>Uploaded on: ' . $upload_date . '</p>';

If you’re looping through multiple attachments (like in a gallery), use this inside your loop:

while (have_posts()) : the_post();
    $upload_date = get_post_time('F j, Y', true);
    echo '<div class="media-item">';
    echo wp_get_attachment_image(get_the_ID(), 'thumbnail');
    echo '<p>Uploaded: ' . $upload_date . '</p>';
    echo '</div>';
endwhile;

Method 2: Pull from Attachment Metadata

If you need the exact timestamp when the file was uploaded (not just the post date), you can fetch it from the attachment’s metadata. This is useful if you’ve edited the post date but want the original upload time:

$attachment_id = 123;
$metadata = wp_get_attachment_metadata($attachment_id);
if (!empty($metadata['image_meta']['created_timestamp'])) {
    $upload_timestamp = $metadata['image_meta']['created_timestamp'];
    $upload_date = date('F j, Y', $upload_timestamp);
    echo '<p>Original Upload Date: ' . $upload_date . '</p>';
}

Note: This works best for images, as other media types might not store the created timestamp in metadata.

Method 3: Modify the Media Library (Backend)

If you want to show the upload date in the WordPress admin media library, you can add a custom column with this snippet (add it to your theme’s functions.php):

function add_upload_date_column($columns) {
    $columns['upload_date'] = 'Upload Date';
    return $columns;
}
add_filter('manage_media_columns', 'add_upload_date_column');

function display_upload_date_column($column_name, $post_id) {
    if ($column_name === 'upload_date') {
        echo get_post_time('F j, Y', true, $post_id);
    }
}
add_action('manage_media_custom_column', 'display_upload_date_column', 10, 2);

Troubleshooting Tips

  • Make sure you’re passing the correct attachment ID—if your template is using a custom query, double-check that get_the_ID() or your variable is targeting the attachment post, not a regular post/page.
  • If get_post_time() returns nothing, verify the attachment has a post date set (it should by default, but sometimes imports or bulk edits can mess this up).
  • Check your WordPress timezone settings (Settings > General) to ensure the date displays in your local time.

内容的提问来源于stack exchange,提问作者GB95

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:28:04