WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display Image Name (12 posts)

  1. walkinman
    Member
    Posted 1 year ago #

    Hey Folks

    I have what I think is a relatively simple question, but I can't seem to find the answer to it. I'm modifying the image attachment page, to display a few things, and I would like to include the image name ...
    Display Image
    caption
    example-image-filename.jpg
    Description

    But I don't see how to get that to happen. Any help would be much appreciated. Thank you.

  2. walkinman
    Member
    Posted 1 year ago #

    PS: The closest I've got is this
    <?php echo wp_get_attachment_url($post->ID); ?>

    which displays the url of the image ... but all I want is the file name, not the url.

    Thanks.

  3. keesiemeijer
    moderator
    Posted 1 year ago #

    Here are some examples of how to get image data on the attachment page:

    echo 'image id = ' . $post->ID;
    echo 'image title = ' . $post->post_title;
    echo 'image caption = ' . $post->post_excerpt;
    echo 'image description = ' . $post->post_content;
    echo 'image alt = ' . get_post_meta($post->ID, '_wp_attachment_image_alt', true);
    
    $imageArray = wp_get_attachment_image_src($post->ID, 'full', false);
    echo 'image uri =' . $imageArray[0]; // 0 is the URI
    echo 'image width =' . $imageArray[1]; // 1 is the width
    echo 'image height =' . $imageArray[2]; // 2 is the height
  4. walkinman
    Member
    Posted 1 year ago #

    hey keesiemeijer

    Thank you for your help. I tried the first line, but it produced the wp ID of the image, not the file name.

    <?php echo echo 'image id = ' . $post->ID; ?>

    correct? That didn't give me the .jpg name, but the id wp assigns on upload.

    I have a feeling that this code
    $imageArray = wp_get_attachment_image_src($post->ID, 'full', false);

    does what I need, but I don't know how to put that in the image.php file in such a way that it produces anything. I'm not a php person, I'm a copy and paster, sorry.

    What I want to display, for ordering purposes, is the .jpg file name of the image.

    Thanks

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    To get the filename you could try this:

    $metadata = wp_get_attachment_metadata( $post->ID );
    if(isset($metadata['file']) && $metadata['file']) {
      echo 'filename = ' . $metadata['file'];
    }
  6. walkinman
    Member
    Posted 1 year ago #

    Hey Keesiemeijer

    Thanks .. closer! That produces the last 2 folders of the url path, plus the file name

    Arrayfilename = 2013/01/07_AUG09711.jpg

    where 2013/01/ are the year/month names of the directory/url. All I want to display is

    07_Aug0971.jpg

    which is the filename. Here's how I inserted the code:

    <?php echo $metadata = wp_get_attachment_metadata( $post->ID );
    if(isset($metadata['file']) && $metadata['file']) {
      echo 'filename = ' . $metadata['file'];
    }
    ?>

    Is that correct?

    Like I said, I'm a hacker.. I hack and stab and snip and cut at it til it bleeds red. :)
    And how do I insert the code so it doesn't say [i]"Arrayfilename = "[/i]?

    Thanks so much for your help.

    Cheers

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    Aha, I see. Hopefully this will work:

    echo 'attachement file name = ' .  basename(get_attached_file( $post->ID ));
  8. keesiemeijer
    moderator
    Posted 1 year ago #

    And how do I insert the code so it doesn't say [i]"Arrayfilename = "[/i]?

    Don't use echo when setting the variable:

    // wrong
    echo $metadata = wp_get_attachment_metadata( $post->ID );
    
    // right
    $metadata = wp_get_attachment_metadata( $post->ID );
  9. walkinman
    Member
    Posted 1 year ago #

    Ha ha .. Dude (assuming you're a "dude") :) .. you're a genius!

    Thanks so much. I really appreciate your help and patience.

    Cheers

  10. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you've got it resolved.
    It seems it was not a simple question after all :-)

  11. alvaro0022
    Member
    Posted 1 year ago #

    Interensante contribution, just what I was looking for. But I can not hacrlo work.

    <?php echo $metadata = wp_get_attachment_metadata( $post->ID ); ?>

    I add the code below to my single.php but I see nothing in the post.

    It may be due to my function.php?

  12. lemuelmas
    Member
    Posted 8 months ago #

    I want to get filenames to show below the thumbs on gallery and on the lightbox. Im using builtin gallery of wordpress. Where do I put the code? Can i show just the name and not the extension of the file?

Topic Closed

This topic has been closed to new replies.

About this Topic