php - Change order of array -
i have code in wordpress gets images attached specific post.
puts information of post in array pulls images using built in wordpress function wp_get_attachment_image
i use foreach loop display images in image slider. problem have is putting 'image1' in position 1 of array, , 'image2' in position 0 of array.
it's displaying image 2 first.
here's code.
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->id ); $attachments = get_posts( $args ); $images = array($attachments); echo '<div id="postslider"><div class="slides_container">'; if ( $attachments ) { foreach ( $attachments $attachment ) { echo '<div>' . wp_get_attachment_image($attachment->id, 'large') . '</div>'; } echo '</div>'; if(sizeof($attachments) > 1) { echo '<div class="slidercontrols"> <a href="#" class="sliderbtnprev">previous</a> <a href="#" class="sliderbtnnext">next</a> <span class="sliderpagination">1 of 3</span> </div>'; } } echo '</div>'; from i've read foreach loop preserve order of array. so, i'm thinking need change order of array loop sees 'image1' (array position [1]) first.
my knowledge of arrays limited , i'm not sure how this.. appreciated.
if want change order programmatically, have @ various array sorting functions in php, especially
uasort()— sort array user-defined comparison function , maintain index associationuksort()— sort array keys using user-defined comparison functionusort()— sort array values using user-defined comparison function
but wordpress checkout http://codex.wordpress.org/template_tags/get_posts
Comments
Post a Comment