WordPress.org

Ready to get started?Download WordPress

Forums

Return an array of attachments to post (2 posts)

  1. n1cked
    Member
    Posted 6 years ago #

    I found a 'resolved' topic here which was very helpful in returning the latest attachment to a post. I want to do a similar thing and retrieve an array of all the attachments to a post.

    The code on the very helpful topic i found is this:

    <?php
    global $wpdb;
    $attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
    ?>

    Followed by several options to then display the properties of that attachment:
    <?php the_attachment_link($attachment_id); ?>
    <?php echo get_attachment_icon($attachment_id); ?>

    What i would like to do is exactly the same but i want to pull a list or array of all attachments to the post then display these attachments as above.

    Any help is appreciated, Thanks.

  2. chosig
    Member
    Posted 5 years ago #

    LIMIT 1

    Limits to one (1) element.
    Remove it show all, or another number to show up to that amount.

Topic Closed

This topic has been closed to new replies.

About this Topic