Support » Fixing WordPress » Calling filename under image

  • I am trying to figure out how to get the image filename and automatically display it underneath any image that is attached to a post.

    I seem to be completely stumped on this one :/

    Any ideas? 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • Just brainstorming here, but I think one way to accomplish this going forward–not for existing posts–would be to create a function in your child theme’s functions.php to override the get_image_tag function in /wp-includes/media.php which would add the HTML to display the file name below the image. You already have the file name in that function ($img_src), so it would just be a matter of adding the HTML with a CSS class to format the file name the way you want.

    Of course, you’d have to edit all your existing posts to add the file name if you went that route.

    If you’re wanting to address this for hundreds of existing posts/images, you’d need to create a function in your child theme’s functions.php to override the get_the_content function in /wp-includes/post-template.php and come up with a regular expression to parse the content of a post, catching all the images, grabbing the value of the “src” attribute, and adding it to the HTML.

    I don’t know how much you know about WordPress, but I keep emphasizing using the functions.php in your child theme because you don’t want to alter the core files or your changes will get overwritten every time you upgrade WordPress. Fortunately, WordPress provides for this by allowing you to create custom functions in the functions.php file of a child theme. That’s what you’d do in either of the above approaches. That way, you could upgrade WordPress and not lose your customizations.

    this apeasr to do it, but it shows the list in a drop down selector

    <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; ?>
    $filename = basename ( get_attached_file( $data->ID ) );
                <?php endforeach;

    @deepbevel I’m not sure that does what damian.smith wants although you a re working along the right lines. Where did you find that code??

    I would do a regular expression replace on the_content to add the image filename after each image HTML tag.

    yes, sorry, got caught up and forgot we need the images..

    I don’t know how this works, not a programmer, just get lucky with finding codes..sometimes.

    @deepbevel Context is important. If you didn’t personally write the code it is better to link to where you got the code from.

    sorry, thought I did

    I think you can take over from here,I just hoped to produce something to work with.

    Thanks for the replies so far guys!
    I may take a look at that code and see what I can do with it too make it work for me!

    I thought there have been a simple answer to this, but obviously a harder one than I thought 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Calling filename under image’ is closed to new replies.