I'm currently developing a wallpaper website using WordPress and I'm trying to figure it out how to retrieve the path of the first attached media in the post so I can display the path in the post (e.g., /wp-content/uploads/image.png). I tried a lot of WordPress functions such as get_attached_media() but I had no luck.

2 Answers 2


I tried to write some snippet with the help of WordPress codex. Please check below and update it further as per your needs. I hope this helps:

global $post;

$args = array(
    'numberposts' => 1,
    'order' => 'ASC',
    'post_mime_type' => 'image',
    'post_parent' => $post->ID,
    'post_status' => null,
    'post_type' => 'attachment',

$attachments = get_children( $args );

if ( $attachments ) {
   foreach ( $attachments as $attachment ) {
      $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'full' );
      echo wp_get_attachment_url($attachment->ID);
      echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="current">';
  • Thank you for your reply. I tried your code and it gives me a blank page with this error in the error log: pastebin.com/nJF0JCkC Here is an exemple of what I'm tring to do: static.dyp.im/jfoq513ojV/84026be18c9e062145d4c41538d43968.PNG
    – Artyum
    Sep 20, 2015 at 15:40
  • The pastebin only shows the error message. Can you please update that with the code in your single.php file ? It does not seem the error which you have added in pastebin is from above code. I can take a look at your single.php code and assist further. Sep 20, 2015 at 15:50
  • 1
    Ah... my bad. There was some extra code at the end of my code snippet. @Kika - thanks for the edits. Sep 20, 2015 at 15:54
  • Thank you, the code worked. In fact, that's not really what I'm trying to achieve, because it doesn't give me the path as I showed you in the previous comment (static.dyp.im/jfoq513ojV/84026be18c9e062145d4c41538d43968.PNG). It gives me this: static.dyp.im/xpj1D938xa/8ebd5dd2e263e02b7b617cb58ffb26f0.PNG
    – Artyum
    Sep 20, 2015 at 16:06

So I finally found out how to display the path of the first attached media in the post. Here is the code I inserted in single.php to display the path at the end of the content :

$image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
$path = parse_url($image_url[0], PHP_URL_PATH);

echo /var/www/wordpress/wp-content/uploads$path;

Result (for exemple): /var/www/wordpress/wp-content/uploads/2015/12/image.jpg

Hope it will help someone else!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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