WordPress.org

Ready to get started?Download WordPress

Forums

Using WP_Query to get attachments (2 posts)

  1. wdm
    Member
    Posted 5 years ago #

    Is there a way to get attachments with WP_Query?

    It seems to work only with get_posts():

    $attachments = new WP_Query('post_type=attachment'); // not working
    
    $attachments = query_posts('post_type=attachment'); // not working
    
    $attachments = get_posts('post_type=attachment'); // works
    
    var_dump($attachments);
  2. MichaelH
    Member
    Posted 5 years ago #

    What about using get_children?

    For example, display thumbnail image for post in loop:

    <?php
    $images = get_children(
    array(
    'post_parent' => $post->ID,
    'post_status' => 'inherit',
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'order' => 'ASC',
    'orderby' => 'menu_order'
    )
    );
    if ( $images ) {
    foreach ( $images as $id => $image ) {
    $img = wp_get_attachment_thumb_url( $image->ID );
    $link = get_permalink( $post->ID );
    print "\n\n" . '<img class="thumb" src="' . $img . '" alt="" />';
    }
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic