Created
August 1, 2012 18:43
-
-
Save ptasker/3229701 to your computer and use it in GitHub Desktop.
Get attachments by slug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Gets the URL to a media item based on the media item's name (slug) | |
* | |
* | |
* @param type $slug | |
* @return type | |
* | |
* | |
*/ | |
function get_attachment_by_slug( $slug ) { | |
if( empty( $slug ) ) | |
return; | |
$query_images_args = array( | |
'post_type' => 'attachment', | |
'post_status' => 'inherit', | |
'name' => $slug | |
); | |
$query_images = new WP_Query( $query_images_args ); | |
$out = array( ); | |
if ( !empty( $query_images->posts ) ) { | |
foreach ( $query_images->posts as $image ) { | |
$out = wp_get_attachment_image_src( $image->ID, 'full' ); | |
} | |
} | |
return isset($out[0])?$out[0]:''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment