Skip to content

Instantly share code, notes, and snippets.

@leoken
Created October 19, 2013 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoken/7056623 to your computer and use it in GitHub Desktop.
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
<?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