16

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.

1 Answer 1

32

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 ) );
2
  • 3
    And here is the link to the discussion
    – kaiser
    Jun 15, 2011 at 15:07
  • 1
    link is dead :( Nov 7, 2014 at 17:22

Your Answer

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

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