I’m using this code to list the image attachments of a post:
<select name="chb_homes_for_sale_specifics_floor_plan" style="width:100%;">
<option value="">Select</option>
<?php
$args = array(
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image'
);
$image = get_posts($args);
if($image) {
foreach($image as $key => $data) : ?>
<option value="<?php echo $data->ID; ?>"><?php echo $data->post_title; ?></option>
<?php endforeach;
}
?>
</select>
But what I’m showing currently is the attachment post_title but I want to show the file name instead. I could maybe use get attachment URL then parse the URL to get the file name but was wondering if there is a ready made way in WordPress to get the file name.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I would strongly advise against using $post->guid – WordPress now generates them in the form;
http:/example.com/?attachment_id=ID
Use the same method that many of the attachment-related functions use;
$filename = basename ( get_attached_file( $data->ID ) );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0