Created
August 27, 2015 15:51
-
-
Save travisfont/f9a6b5b8349c0baee453 to your computer and use it in GitHub Desktop.
Wordpress - How to get a post attachments and post attachment images: get_post_attachments() & get_post_attachment_images()
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 get_post_attachments($post_parent_id, $numberpost = -1, $posts_status = NULL) | |
| { | |
| $args = array | |
| ( | |
| 'post_type' => 'attachment', | |
| 'numberposts' => $numberpost, | |
| 'post_status' => $posts_status, | |
| 'post_parent' => $post_parent_id | |
| ); | |
| return get_posts($args); | |
| } | |
| function get_post_attachment_images($post_parent_id, $attr = NULL, $size = 'full', $posts_status = NULL) | |
| { | |
| $attachment_images = array(); | |
| $args = array | |
| ( | |
| 'post_type' => 'attachment', | |
| 'numberposts' => -1, | |
| 'post_status' => $posts_status, | |
| 'post_parent' => $post_parent_id | |
| ); | |
| if ($attachments = get_posts($args)) | |
| { | |
| foreach ($attachments as $attachment) | |
| { | |
| $attachment_images[] = wp_get_attachment_image($attachment->ID, $size, FALSE, $attr); | |
| } | |
| } | |
| return $attachment_images; | |
| } | |
| $query = new WP_Query($args); | |
| if ($query->have_posts()) | |
| { | |
| while ($query->have_posts()) | |
| { | |
| $query->the_post(); | |
| $attributes = array('class' => 'slider-small', 'alt' => esc_attr(get_the_title())); | |
| $attachments = get_post_attachment_images(get_the_ID(), $attributes); | |
| print_r($attachments); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment