Created
October 19, 2013 14:27
-
-
Save leoken/7056623 to your computer and use it in GitHub Desktop.
Show the first image associated with the post
This function retrieves the first image associated with a post From http://codex.wordpress.org/Function_Reference/get_children#Examples
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 | |
function echo_first_image( $postID ) { | |
$args = array( | |
'numberposts' => 1, | |
'order' => 'ASC', | |
'post_mime_type' => 'image', | |
'post_parent' => $postID, | |
'post_status' => null, | |
'post_type' => 'attachment', | |
); | |
$attachments = get_children( $args ); | |
if ( $attachments ) { | |
foreach ( $attachments as $attachment ) { | |
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' ); | |
echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="current">'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment