Last active
August 29, 2015 13:57
-
-
Save pedrorvidal/9665535 to your computer and use it in GitHub Desktop.
Get all images attached to post
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
/* | |
* Get all images EXCEPT post thumbnail | |
*/ | |
<?php if ( $post->post_type == 'data-design' && $post->post_status == 'publish' ) { | |
$attachments = get_posts( array( | |
'post_type' => 'attachment', | |
'posts_per_page' => -1, | |
'post_parent' => $post->ID, | |
'exclude' => get_post_thumbnail_id() | |
) ); | |
if ( $attachments ) { | |
foreach ( $attachments as $attachment ) { | |
$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type ); | |
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true ); | |
echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>'; | |
} | |
} | |
} | |
?> |
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
/** | |
* Gets all images attached to a post | |
* @return string | |
*/ | |
function wpse_get_images() { | |
global $post; | |
$id = intval( $post->ID ); | |
$size = 'medium'; | |
$attachments = get_children( array( | |
'post_parent' => $id, | |
'post_status' => 'inherit', | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'order' => 'ASC', | |
'orderby' => 'menu_order' | |
) ); | |
if ( empty( $attachments ) ) | |
return ''; | |
$output = "\n"; | |
/** | |
* Loop through each attachment | |
*/ | |
foreach ( $attachments as $id => $attachment ) : | |
$title = esc_html( $attachment->post_title, 1 ); | |
$img = wp_get_attachment_image_src( $id, $size ); | |
$output .= '<a class="selector thumb" href="' . esc_url( wp_get_attachment_url( $id ) ) . '" title="' . esc_attr( $title ) . '">'; | |
$output .= '<img class="aligncenter" src="' . esc_url( $img[0] ) . '" alt="' . esc_attr( $title ) . '" title="' . esc_attr( $title ) . '" />'; | |
$output .= '</a>'; | |
endforeach; | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment