0

I have a product image catalogue and i'd like to display the Uploaded date of the image:

I can see on the media library it displays on-top of the item: Uploaded on: April 15, 2021 as an exmaple but how do I access that? in my templated files

2 Answers 2

0

If you have the attachment ID, you can just use get_the_date():

<?php echo get_the_date( '', $attachment_id ); ?>

If you leave the first argument blank, e.g. '', then it will use the date format from settings. If you don't want that, then provide a PHP date format.

0

You can use the date_i18n function to format the date according to the WP date settings.

// Assuming you have the attachment ID
$attachment_id = 123; // Replace this with your actual attachment ID

// Get the attachment post object
$attachment_post = get_post($attachment_id);

if ($attachment_post) {
    // Get the uploaded date
    $uploaded_date = $attachment_post->post_date;

    // Format the date
    $formatted_date = date_i18n(get_option('date_format'), strtotime($uploaded_date));

    // Display the date
    echo 'Uploaded on: ' . esc_html($formatted_date);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.