WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Output the image's filename only (6 posts)

  1. surfsup74
    Member
    Posted 1 year ago #

    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).

  2. wpismypuppet
    Member
    Posted 1 year ago #

    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); ?>

  3. surfsup74
    Member
    Posted 1 year ago #

    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; ?>
  4. wpismypuppet
    Member
    Posted 1 year ago #

    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;
    ?>
  5. surfsup74
    Member
    Posted 1 year ago #

    Perfect. Thanks wpismypuppet.

  6. wpismypuppet
    Member
    Posted 1 year ago #

    You are most welcome!

Topic Closed

This topic has been closed to new replies.

About this Topic