Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm trying to get a list of different image sizes in Wordpress. I think this is more of a php question though so I'm asking it here instead of on the wordpress forum.

Here is my code:

function ajax_get_latest_posts(){   
   $attachments = get_children(array('post_parent' => $post->ID,
                    'post_status' => 'inherit',
                    'post_type' => 'attachment',
                    'post_mime_type' => 'image',
                    'order' => 'ASC',
                    'orderby' => 'menu_order ID'));

   foreach($attachments as $att_id => $attachment) {
      $large_bg_url = wp_get_attachment_image_src($attachment->ID, 'background-large');
      $medium_bg_url = wp_get_attachment_image_src($attachment->ID, 'background-medium');
      $small_bg_url = wp_get_attachment_image_src($attachment->ID, 'background-small');
      $imagesizes .= array('background_large' => $large_bg_url[0], 'background_medium' => $medium_bg_url[0], 'background_small' => $small_bg_url[0]);
   }

   $allimagesizes = array($imagesizes);

   return $allimagesizes;
}

The above code does not work but hopefully shows what I'm trying to achieve. The problem is the concatenation of $imagesizes is not right. Below is the output I actually need to achieve but I cannot seem to figure out how to get this output from the foreach loop.

$allimagesizes = array(
   array(
      'background_large' => 'http://mydomain.com.au/wp-content/uploads/facade.jpg', 
      'background_medium' => 'http://mydomain.com.au/wp-content/uploads/facade-1366x807.jpg', 
      'background_small' => 'http://mydomain.com.au/wp-content/uploads/facade-1024x605.jpg '
   ),
   array(
      'background_large' => 'http://mydomain.com.au/wp-content/uploads/facade-trees.jpg', 
      'background_medium' => 'http://mydomain.com.au/wp-content/uploads/facade-trees-1366x818.jpg', 
      'background_small' => 'http://mydomain.com.au/wp-content/uploads/facade-trees-1024x613.jpg '
   )    
 );
share|improve this question

2 Answers 2

up vote 0 down vote accepted

You almost had it.

function ajax_get_latest_posts(){   
   $attachments = get_children(array('post_parent' => $post->ID,
                    'post_status' => 'inherit',
                    'post_type' => 'attachment',
                    'post_mime_type' => 'image',
                    'order' => 'ASC',
                    'orderby' => 'menu_order ID'));

   $allimagesizes = array();
   foreach($attachments as $att_id => $attachment) {
      $large_bg_url = wp_get_attachment_image_src($attachment->ID, 'background-large');
      $medium_bg_url = wp_get_attachment_image_src($attachment->ID, 'background-medium');
      $small_bg_url = wp_get_attachment_image_src($attachment->ID, 'background-small');
      $imagesizes = array('background_large' => $large_bg_url[0], 'background_medium' => $medium_bg_url[0], 'background_small' => $small_bg_url[0]);
      $allimagesizes[] = $imagesizes;
   }
   return $allimagesizes;
}

Should work.

First off we defined the array before the foreach.

Remove the concatenation operator from the imagesizes assignment.

Next we are pushing the array created in the loop into the $allimagessizes array inside the loop.

share|improve this answer
    
Thanks heaps. Although when I do $allimagesizes[] I get an extra set of [] around my json data which is breking the output... any way to remove this extra set of []? – nicole2292 Jul 19 '13 at 6:09
    
@nicole2292 edited the answer... that should get you what you want .... basically I dropped the array() wrapping the $imagesizes array when doing the assignment at the end of the loop – Orangepill Jul 19 '13 at 6:13
    
Perfect thank you! I could have drilled an extra level in my jQuery but I think this is better :) – nicole2292 Jul 19 '13 at 6:19

Use this:

$allimagesizes[] = $imagesizes;

Putting [] after the array name means to add a new element to the end of the array.

Also, in the assignment to $imagesizes, you should use =, not .=. The latter is for appending to a string.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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