Image Function Cheat Sheet

Simply a reference of the six image-related functions I use most often. I have this list printed out and keep it handy; for your convenience I’ve linked to the WP doc page for each.

In case you weren’t aware, many of these accept either a named size (e.g. ‘small’,'medium’,'full’,'your_custom_name’) or an array of [width,height] (e.g. array(300,200)).

wp_get_attachment_image

wp_get_attachment_image( $attachment_id, $sizename_or_array, $icon, $attr );

Returns HTML tag, something like <img src="http://xyz …" >

get_the_post_thumbnail

get_the_post_thumbnail( $optional_post_id, $sizename_or_array, $attr );

Returns HTML tag, something like <img src="http://xyz …" >

get_post_thumbnail_id

get_post_thumbnail_id( $optional_post_id );

Returns ID of attachment, something like 123

wp_get_attachment_image_src

wp_get_attachment_image_src( $attachment_id, $sizename_or_array, $icon );

Returns array containing [source url, width, height], something like array('http://xyz.com…', 300, 200)

wp_get_attachment_url

wp_get_attachment_url( $attachment_id );

Returns image URL, something like 'http://xyz.com…'

get_attached_file

get_attached_file( $attachment_id, $unfiltered=false );

Returns image filepath, something like '/home/dirname/public_html/wp-content/uploads/2014/12/xyz.jpg'