WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Output the image's filename only

[Resolved] Output the image's filename only

  • I would like to insert image file names into the ‘title’ attribute within the ‘a href=’ that is wrapped around my images. For example: <a href="example-url" title="This image is named XXXX.jpg"><img src="path-to-image/XXXX.jpg" /></a>

    I can’t find a way to get the file name only (the ‘XXXX.jpg’ part – without the path).

Viewing 5 replies - 1 through 5 (of 5 total)
  • wpismypuppet

    @wordpressismypuppet

    Are you already getting the URL of the image? Can you show a little code? If you already have the URL of the image in a variable like:

    <?php $image = pathofimage; ?>

    Then you can use the PHP function basename() to get just the filename. Like this (using $image from above):

    <?php $filename = basename($image); ?>

    Hi wpismypuppet,

    I can get the url using <?php echo $image[0]; ?> as in the code below. However, how do I combine that with your suggestion in order to put just the filename in place of ‘XXXX’ in my code below?

    <?php // start advanced custom fields repeater
    					if(get_field('images')): ?>
                    	<?php while(the_repeater_field('images')): ?><?php $image = wp_get_attachment_image_src(get_sub_field('image'), 'full'); ?>
    					<a href="<?php echo $image[0]; ?>"  rel="shadowbox[set]" title="Click to view this photograph fullscreen. Filename: XXXX" >
    					<img src="<?php echo $image[0]; ?>" width="678" /></a>
            			<?php endwhile; ?>
        				<?php endif; ?>
    wpismypuppet

    @wordpressismypuppet

    Here is your code combined with mine (P.S. I stripped a lot of repeating <?php ?> as you don’t need them every time!). Best of luck!

    <?php
    	// start advanced custom fields repeater
    	if(get_field('images')):
    		while(the_repeater_field('images')):
    			$image = wp_get_attachment_image_src(get_sub_field('image'), 'full');
    			echo '<a href="' . $image[0] . '"  rel="shadowbox[set]" title="Click to view this photograph fullscreen. Filename: ' . basename( $image[0] ) . '">';
    			echo '<img src="' . $image[0] . '" width="678" /></a>';
    		endwhile;
    	endif;
    ?>

    Perfect. Thanks wpismypuppet.

    wpismypuppet

    @wordpressismypuppet

    You are most welcome!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Output the image's filename only’ is closed to new replies.
Skip to toolbar