0

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.

2 Answers 2

1

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.

2
  • Nothing happens when I put this code in single.php file... Commented Dec 20, 2012 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
    Commented Dec 20, 2012 at 16:34
0

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; 
?>
2
  • The question is not about how to get custom fields data but rather getting data about the image.
    – Ashfame
    Commented Dec 20, 2012 at 14:38
  • After inserting that code in my single.php file nothing happens. Am I doing something wrong?? Commented Dec 20, 2012 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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