Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I’m doing up a photography theme in WP at the moment, and I’ve already got great help on this thread (thanks Milo!) for a previous question.

In that thread, I asked about displaying the file name of an uploaded file on the attachment.php page for that file. I’m now using the suggested answer, which uses this code to display the name: <?php echo basename( wp_get_attachment_url( $post->ID ) ); ?>

What I need to do, however, is to display this file without its extension: so, filename, instead of filename.jpg. Is there any easy tweak to this existing PHP snippet that could do this? If not, what would be a best-practice way of achieving this end result?

I should add that I’m far from a PHP expert, so take it easy on me... :)

Thanks.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You could use substr()

Returns the portion of string specified by the start and length parameters.

in combination with strrpos()

Find the numeric position of the last occurrence of needle in the haystack string.

Example

$name = basename( wp_get_attachment_url( $post->ID ) );
echo substr( $name, 0, strrpos( $name, '.' ) );

This will show the string (in this case a filename) untill the last occurance of ..

share|improve this answer
    
That did it – thanks! –  kopfschmerz Mar 25 '13 at 17:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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