Skip to content

Instantly share code, notes, and snippets.

@lunaroja
Created July 4, 2013 04:26
Show Gist options
  • Save lunaroja/5924922 to your computer and use it in GitHub Desktop.
Save lunaroja/5924922 to your computer and use it in GitHub Desktop.
Wordpress: Get post thumbnail URLs as data attributes
// use inside the loop
// <div <?php post_class() ?> <?php post_thumbnail_data_attributes(); ?>>
// data-thumbnail-large="http://url.com/wp-content/uploads/2013/07/chicago-1000x703.jpg"
// data-thumbnail-medium="http://url.com/wp-content/uploads/2013/07/chicago-500x351.jpg"
function post_thumbnail_data_attributes() {
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_large = wp_get_attachment_image_src($thumbnail_id, 'large');
$data_attributes = ' data-thumbnail-large="' . $thumbnail_large[0] .'" ';
$thumbnail_medium = wp_get_attachment_image_src($thumbnail_id, 'medium');
$data_attributes .= ' data-thumbnail-medium="' . $thumbnail_medium[0] .'" ';
echo $data_attributes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment