Skip to content

Instantly share code, notes, and snippets.

@travisfont
Created August 27, 2015 15:51
Show Gist options
  • Select an option

  • Save travisfont/f9a6b5b8349c0baee453 to your computer and use it in GitHub Desktop.

Select an option

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()
<?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