WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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>
        $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;

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.

share|improve this question
up vote 16 down vote accepted

I would strongly advise against using $post->guid - WordPress now generates them in the form;


Use the same method that many of the attachment-related functions use;

$filename = basename ( get_attached_file( $data->ID ) );
share|improve this answer
And here is the link to the discussion – kaiser Jun 15 '11 at 15:07
link is dead :( – simonthesorcerer Nov 7 '14 at 17:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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