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.

What I need is some function to echo file name of image used in post like "image-file-name.jpg", or just "image-file-name".

Also it would be great to be able to have even more information about image like:

  • image width,
  • image height,
  • image mime type / format,
  • image file size,
  • image extension.

Also this may be helpful, I have custom field named 'image_full_url' which contains full url address of image used in post. For example content of custom field named 'image_full_url' is:

http://www.example.com/wp-content/uploads/2012/12/image-file-name.jpg

I am using that custom field to echo that URL when I need it, and it would be nice to have something similar to echo other data about image.

share|improve this question
add comment

2 Answers 2

You can get the file from your Url like this:

//get the URL from your Metafield, $post is your current Post
$image = get_post_meta( $post->ID, '_your_image_url_meta', true );

//set up a query to search your posts table for the Url         
global $wpdb;
$query = "SELECT ID FROM $wpdb->posts WHERE guid = %s LIMIT 1";

//prepare and launch the query
$result = $wpdb->get_results( $wpdb->prepare( $query, $image ) );

//if there are results
if ( count ( $result ) > 0 ) {

    $image_id = $result['0']->ID;

    //get the image data
    $image_data = get_post_meta( $image_id, '_wp_attachment_metadata', true );

}   

All the information you need should now be available in $image_data. Keep in mind that you may need to change the script a little bit - for example if your Authors put in a thumbnail-url, you have to clean it first and change the query to a LIKE statement.

share|improve this answer
    
Nothing happens when I put this code in single.php file... –  user1038179 Dec 20 '12 at 15:37
    
Did you change the key of the metafield? Can you echo the $image variable? Also note that this code does not produce any output.. –  fischi Dec 20 '12 at 16:34
add comment

If you have declare every field of image in custom field. Use following code for getting all post custom feild data;

<?php 
 while ( have_posts() ) : the_post(); 
 $postid=$post->ID;
 $all_meta_for_post = get_post_meta( $postid );    
 endwhile; 
?>
share|improve this answer
    
The question is not about how to get custom fields data but rather getting data about the image. –  Ashfame Dec 20 '12 at 14:38
    
After inserting that code in my single.php file nothing happens. Am I doing something wrong?? –  user1038179 Dec 20 '12 at 15:29
add comment

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.