Get the URL of an Image in WordPress


Categories
Tags
When building or editing a WordPress theme, you might want to get the URL of a specific image by it’s ID. Here’s how to do it using the ‘wp_get_attachment_url’ function.

Using PHP and the very handy in-built WordPress function, ‘wp_get_attachment_url’, this is really easy to do. Simply change the ID shown in brackets below to the ID of the image that you want to get the URL of.

We’ve put the returned value into a variable, so you can display this on your page with a simple echo.

// the number in brackets is the ID of the image
$image_url = wp_get_attachment_url( '6' );
// display the URL on screen
echo $image_url;

This would display something like: https://burst.shopify.com/photos/developer-coding-in-php

Learn more about the wp_get_attachment_url function on the WordPress codex site.

Comments

We welcome comments! Please read the disclaimer.

Disclaimer and Legal Information

The use of any content found on or via this website (code, how to guides etc.) is done so at your own risk. We take no responsibility for any issues encountered.

This blog and its authors will not be held responsible for any misuse, reuse, recycled and cited/uncited copies of content from this website by others.

The views and opinions expressed in this blog are those of the authors and do not necessarily reflect the position or opinion of any other agency, organisation, employer or company.

Comments are moderated but we do not take any responsibility for any libel or litigation that results from something written in a comment. We reserve the right to reject or delete any comment for any reason whatsoever (abusive, profane, rude etc). Please keep your comments polite and relevant.

We are happy for you to quote and share our content in any reasonable manner, e.g. post links to our blogs on social media, but not in any way that suggests that we, or our authors, endorse you, your use or your views.

We appreciate attributions, e.g. a link to our website (www.dragoncode.co.uk).