GSMtricks
GSMtricks

Reputation: 31

Function - How to get images attached to post?

I am planing to use one slider, which use shortcode:

[su_carousel source="media: 160,161,162" limit="10" link="image" height="300" title="no"]

This 160,161,162 is IDs of images attached to that post. So I need some automatic function which can give me array with Ids of images attached to post.

I am using wpallimport plugin, and I need to build template for all posts, now I can import all images related to post, but don't have a function which allow me to get images IDs.

Any help there?

Upvotes: 0

Views: 310

Answers (1)

Ilya
Ilya

Reputation: 52

You can use get_posts():

$args = array( 
    'post_type' => 'attachment', 
    'posts_per_page' => -1, 
    'post_status' =>'any', 
    'post_parent' => $post->ID
); 
$attachments = get_posts( $args );
if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        $attachments_ids_array[] = $attachment->ID;
    }
}

$attachments_ids = implode (", ", $attachments_ids_array)

Upvotes: 1

Related Questions