0

I'm using wp_query to get all attachments, and it works, but I wonder how could I get attachments in full size because this query returns images in medium.

<?php 
    $args = array('post_status' => 'inherit', 'post_type' => 'attachment', 'posts_per_page' => 5 );
    $wp_query = new WP_Query($args);
    while (have_posts() ) : the_post(); ?>

        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <?php
                the_title( '<h2>', '</h2>' );
                the_content();
            ?>
        </article>

    <?php endwhile; ?>

3 Answers 3

0

If you look at the raw post data...

while (have_posts() ) { 
  the_post(); ?>
  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>><?php
      var_dump($post); ?>
  </article><?php 
}

You will see that there is nothing in there to specify "medium", so I went looking. There is a filter on the_content...

add_filter( 'the_content', 'prepend_attachment' );

... that juggles data for "attachment" post types.

1576    function prepend_attachment($content) {
1577            $post = get_post();
1578    
1579            if ( empty($post->post_type) || $post->post_type != 'attachment' )
1580                    return $content;
1581    
1582            if ( wp_attachment_is( 'video', $post ) ) {
1583                    $meta = wp_get_attachment_metadata( get_the_ID() );
1584                    $atts = array( 'src' => wp_get_attachment_url() );
1585                    if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
1586                            $atts['width'] = (int) $meta['width'];
1587                            $atts['height'] = (int) $meta['height'];
1588                    }
1589                    if ( has_post_thumbnail() ) {
1590                            $atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() );
1591                    }
1592                    $p = wp_video_shortcode( $atts );
1593            } elseif ( wp_attachment_is( 'audio', $post ) ) {
1594                    $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
1595            } else {
1596                    $p = '<p class="attachment">';
1597                    // show the medium sized image representation of the attachment if available, and link to the raw file
1598                    $p .= wp_get_attachment_link(0, 'medium', false);
1599                    $p .= '</p>';
1600            }
1601    
1602            /**
1603             * Filter the attachment markup to be prepended to the post content.
1604             *
1605             * @since 2.0.0
1606             *
1607             * @see prepend_attachment()
1608             *
1609             * @param string $p The attachment HTML output.
1610             */
1611            $p = apply_filters( 'prepend_attachment', $p );
1612    
1613            return "$p\n$content";
1614    }

The relevant lines are 1597 and 1598. wp_get_attachment_link(), which in turn uses wp_get_attachment_image() (line 1545), which uses image_downsize()... and there is a promising filter:

167         if ( $out = apply_filters( 'image_downsize', false, $id, $size ) ) {
168                 return $out;
169         }

Et viola!

function image_size_hack() {
  $img_url = wp_get_attachment_url();
  $meta = wp_get_attachment_metadata();
  $width= $height = '';
  if (!empty($meta['width'])) {
    $width = $meta['width'];
  }
  if (!empty($meta['height'])) {
    $width = $meta['height'];
  }
  return array( $img_url, $width, $height, false );
}
add_filter( 'image_downsize',  'image_size_hack');
1

You can customize the wp_get_attachment_image function as per your need. pass the image size you want to display.

wp_get_attachment_image( $attachment->ID, 'full' );

more info at: https://codex.wordpress.org/Function_Reference/wp_get_attachment_image

1
  • This is the correct answer, just echo the wp_get_attachment_image at any size you want
    – Rich
    Commented May 23, 2016 at 17:09
0

You should be able to set your own image tag. You can replace the_content(); with this.

<img src="<?= wp_get_attachment_url(get_the_ID()); ?>"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.