-
-
Save misfist/6dc2f963e345160a4a60a424b5026c27 to your computer and use it in GitHub Desktop.
WordPress - Get a first image attachment in post
This file contains hidden or 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 | |
function prefix_get_first_attachment( $post ) { | |
$attachment = get_children( | |
array( | |
'post_parent' => $post->ID, | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'order' => 'DESC', | |
'numberposts' => 1, | |
) | |
); | |
if ( empty( $attachment ) || is_wp_error( $attachment ) ) { | |
return false; | |
} | |
$attachment = current( $attachment ); | |
return $attachment; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment