Skip to content
This repository has been archived by the owner on Nov 8, 2019. It is now read-only.

Commit

Permalink
Helper function for getting attachment by its name (closes #37)
Browse files Browse the repository at this point in the history
  • Loading branch information
literat committed Oct 25, 2015
1 parent e737ff0 commit e4ba1a8
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions inc/extras.php
Expand Up @@ -84,6 +84,29 @@ function dswoddil_wp_title( $title, $sep ) {

endif;

/**
* Finds attachment by its name.
*
* @param string $post_name Name of the attachment.
* @return array Attachment properties.
*/
if( ! ( function_exists( 'wp_get_attachment_by_post_name' ) ) ) {
function dswoddil_get_attachment_by_post_name( $post_name ) {
$args = array(
'post_per_page' => 1,
'post_type' => 'attachment',
'name' => trim ( $post_name ),
);
$get_posts = new Wp_Query( $args );

if ( ! empty ( $get_posts->posts[0] ) ) {
return $get_posts->posts[0];
} else {
return false;
}
}
}

/******************************************************************************
PLUGINS
******************************************************************************/
Expand Down

0 comments on commit e4ba1a8

Please sign in to comment.