Title: Getting attachment width for medium size
Last modified: August 19, 2016

---

# Getting attachment width for medium size

 *  [Matthew](https://wordpress.org/support/users/existentialmedia/)
 * (@existentialmedia)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/getting-attachment-width-for-medium-size/)
 * I’m working on a theme with a side scrolling image slideshow. I’m trying to set
   the container width dynamically. I’m trying to grab the image widths of all the
   images in the post/page and add them up with their padding.
 * I’m able to get the original attachment width with:
 *     ```
       $attachment_width = wp_get_attachment_metadata($attachment_id);
       echo $attachment_width['width'];
       ```
   
 * But **wp_get_attachment_metadata** does not seem to return the width for the 
   medium size of the images.
 * I tried also to use the php function **[getimagesize](http://php.net/manual/en/function.getimagesize.php)**
   to get the image widths, but I get errors with this code:
 *     ```
       $attachment_width = wp_get_attachment_image_src($attachment_id, $size='medium');
       list($width, $height, $type, $attr) = getimagesize($attachment_width);
       echo $width;
       ```
   
 * For clarity, here is the full code I’m using for scrolling image slideshow from
   the functions.php:
 *     ```
       // Add lightbox rel
       function wp_get_attachment_link_canoe($id = 0, $size = 'thumbnail', $permalink = false, $icon = false) {
       	$_post = & get_post( intval($id) );
       	if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) )
       		return __('Missing Attachment');
       	if ( $permalink )
       		$url = get_attachment_link($_post->ID);
       	$post_title = attribute_escape($_post->post_title);
       	$link_text = wp_get_attachment_image($id, $size, $icon);
       	if ( !$link_text )
       		$link_text = $_post->post_title;
       	return "<a rel='lightbox[$_post->post_parent]' href='$url' title='$post_title'>$link_text</a>";
       }
   
       // Scroll shortcode
       function scroll(){
       echo '<div class="scroll">';
       $attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'order_by' => 'menu_order ID') );
       foreach ( $attachments as $attachment_id => $attachment ) {
       	?><?php echo wp_get_attachment_link_canoe($attachment_id,$size='medium'); ?><?php
       /*
       	$attachment_width = wp_get_attachment_metadata($attachment_id);
       	echo $attachment_width['width'];
   
       	$attachment_width = wp_get_attachment_image_src($attachment_id, $size='medium');
       	list($width, $height, $type, $attr) = getimagesize($attachment_width);
       	echo $width;
       */
       }
       echo '</div>';
       }
       add_shortcode('scroll','scroll');
       ```
   
 * I want to set the width of the class “scroll”. Temporarily I’ve set it statically
   very large. But this inhibits the usability of the side scroll.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [Matthew](https://wordpress.org/support/users/existentialmedia/)
 * (@existentialmedia)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/getting-attachment-width-for-medium-size/#post-1591948)
 * There is a plugin for [Indexhibit](http://indexhibit.org/) that I came across
   that works like this. In Indexhibit the image sizes are stored in the database
   when you upload them. The plugin **Horizontal Format** grabs the sizes from the
   db and adds them up.
 *     ```
       $pages = $OBJ->db->fetchArray("SELECT *
       	FROM ".PX."media, ".PX."objects_prefs
       	WHERE media_ref_id = '$rs[id]'
       	AND obj_ref_type = 'exhibit'
       	AND obj_ref_type = media_obj_type
       	ORDER BY media_order ASC, media_id ASC");
       ```
   
 *     ```
       foreach ($pages as $go)
       {
       	$title = ($go['media_title'] == '') ? '' : "<div class='title'>" . $go['media_title'] . "</div>";
       	$title .= ($go['media_caption'] == '') ? '' : "<div class='caption'>" . $go['media_caption'] . "</div>";
   
       	$temp_x = $go['media_x'] + $this->picture_block_padding_right;
       	$this->final_img_container += ($go['media_x'] + $this->picture_block_padding_right);
   
       	$a .= "<div class='picture_holder' style='width: {$temp_x}px;'>\n";
       	$a .= "<div class='picture' style='width: {$go[media_x]}px;'>\n";
       	$a .= "<img src='" . BASEURL . GIMGS . "/$go[media_file]' width='$go[media_x]' height='$go[media_y]' alt='" . BASEURL . GIMGS . "/$go[media_file]' />\n";
       	$a .= "<div class='captioning'>$title</div>\n";
       	$a .= "</div>\n";
       	$a .= "</div>\n\n";
       }
   
       $s .= "<div id='img-container'>\n";
       if ($rs['content'] != '') $s .= "<div id='text'>" . $rs['content'] . "</div>\n";
       $s .= $a;
       $s .= "<div style='clear: left;'><!-- --></div>";
       $s .= "</div>\n";
   
       return $s;
       }
       ```
   
 * Maybe is there a way to do this with WP? I imagine that the image sizes are stored
   in the DB as well. This is maybe a better route than using **getimagesize**.
 *  Thread Starter [Matthew](https://wordpress.org/support/users/existentialmedia/)
 * (@existentialmedia)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/getting-attachment-width-for-medium-size/#post-1592099)
 * OKAY I FIGURED IT OUT.
 * For those interested in this problem:
 *     ```
       // Scroll shortcode
       function scroll_shortcode(){
       	$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'DESC', 'orderby' => 'menu_order') );
       	foreach ( $attachments as $attachment_id => $attachment ) {
       		$attachment_meta = wp_get_attachment_metadata($attachment_id);
       		$attachment_width = $attachment_meta['sizes']['medium']['width'] + 30;
       		$width += $attachment_width;
       	};
       	echo '<div class="scroll" style="width:'.$width.'px !important;">';
   
       foreach ( $attachments as $attachment_id => $attachment ) {
       	?><?php echo wp_get_attachment_link_canoe($attachment_id,$size='medium'); ?><?php
       }
       echo '</div>';
       }
       add_shortcode('scroll','scroll_shortcode');
       ```
   
 *  Thread Starter [Matthew](https://wordpress.org/support/users/existentialmedia/)
 * (@existentialmedia)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/getting-attachment-width-for-medium-size/#post-1592100)
 * Whoops, it needed a fallback if there is no medium size:
 *     ```
       // Scroll shortcode
       function scroll_shortcode(){
       	$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'DESC', 'orderby' => 'menu_order') );
       	foreach ( $attachments as $attachment_id => $attachment ) {
       		$attachment_meta = wp_get_attachment_metadata($attachment_id);
       		if (isset($attachment_meta['sizes']['medium']['width'])) {
       			$attachment_width = $attachment_meta['sizes']['medium']['width'] + 30;
       			$scroll_width += $attachment_width;
       		} else {
       			$attachment_width = $attachment_meta['width'] + 30;
       			$scroll_width += $attachment_width;
       		}
       	};
       	echo '<div class="scroll" style="width:'.$scroll_width.'px !important;">';
   
       foreach ( $attachments as $attachment_id => $attachment ) {
       	?><?php echo wp_get_attachment_link_canoe($attachment_id,$size='medium'); ?><?php
       }
       echo '</div>';
       }
       add_shortcode('scroll','scroll_shortcode');
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Getting attachment width for medium size’ is closed to new replies.

## Tags

 * [getimagesize](https://wordpress.org/support/topic-tag/getimagesize/)
 * [horizontal scroll](https://wordpress.org/support/topic-tag/horizontal-scroll/)
 * [wp_get_attachment_image()](https://wordpress.org/support/topic-tag/wp_get_attachment_image/)
 * [wp_get_attachment_link](https://wordpress.org/support/topic-tag/wp_get_attachment_link/)

 * 3 replies
 * 1 participant
 * Last reply from: [Matthew](https://wordpress.org/support/users/existentialmedia/)
 * Last activity: [15 years, 6 months ago](https://wordpress.org/support/topic/getting-attachment-width-for-medium-size/#post-1592100)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
