Support » Fixing WordPress » Control sort order when using wp_get_attachment_image ?

  • I’m using the WP function ‘wp_get_attachment_image’ to pull images from my page’s gallery.

    The function works fine, however the images pull from newest to oldest. Is there a way to control the sort order? For example, I’d like to pull the images in ascending order as they are listed in the page’s media gallery attachments.

    Thanks!

Viewing 12 replies - 1 through 12 (of 12 total)
  • Christine Rondeau

    (@crondeau)

    Volunteer Forum Moderator

    In your page, in the image uploader, if you click on gallery, you should be able to set the order of the images by adding a number to each.

    Once you added these, that should change the order.

    Hello Christine,

    The images are numbered in the order I want them to appear, but it’s have no effect for how they appear on my page. Here’s the code I’m using:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    esmi

    (@esmi)

    Forum Moderator

    The ordering in the Gallery is just for the Gallery and nothing else. wp_get_attachment_image() doesn’t pull images from the Gallery. It grabs all image attachments for a given Post or Page. If you want to control the order that the images are returned in, you’d probably be better off using get_posts('post_type=attachment').
    http://codex.wordpress.org/Template_Tags/get_posts

    Christine Rondeau

    (@crondeau)

    Volunteer Forum Moderator

    That’s weird your code does look good. I haven’t used get_children myself and the codex suggest that get_post may be easier.

    Here is the code that I use on one of my sites:

    <ul>
    <?php if (have_posts()) : while (have_posts()) : the_post();
        $args = array(
        'post_type' => 'attachment',
         'numberposts' => -1,
         'post_status' => null,
         'post_parent' => $post->ID
      );
        $attachments = get_posts($args);
    	if ($attachments) {
    		foreach ($attachments as $attachment) {
    		print "\n";
    		echo '<li>';
    		echo wp_get_attachment_image($attachment->ID, 'full');
    		echo '<p>';
    		echo apply_filters('the_title', $attachment->post_title);
    		echo '</p></li>';
    	}
    }
    endwhile; endif; ?>
    	</ul>

    It doesn’t specify the order either, but it works.. Maybe try using get_post() instead?

    Times like this I wish I had more time to learn the ins and out of php and wordPress. 🙁

    Christine Rondeau

    (@crondeau)

    Volunteer Forum Moderator

    Ha… I was typing at the same time as esmi.. Her explanation makes sense and confirms what I thought.

    I wanted to clarify that I only want to pull images on a specific page (this is being used in a page template). Will get_posts still work?

    Christine Rondeau

    (@crondeau)

    Volunteer Forum Moderator

    Yes, it should work on a page too, that’s where I’ve used it.

    I have the same problem and after a long search, I found this post.
    Nice! 🙂

    I am using this code to query attached images, but I want to use the gallery order of the attatched images to a post, how may I achieve this?

    I know that ‘order’ => ‘gallery order’ would be a nice fictional solution.

    [Code moderated as per the Forum Rules. Please use the pastebin]

    As far as I know the only parameters I know of are
    ‘orderby’ => ‘post_date’,….
    ‘order’ => ‘DESC’,….

    I came up against this same situation today I think.

    First thing to do is to add the the following parameter to the $args array that you are passing to get_posts()…

    ‘orderby’ => ‘menu_order’

    This will sort the returned array of attachments in relation to the gallery order you’re requesting (if I’ve understood correctly).

    However, when you do the above you’ll notice that the results are actually output in descending gallery order which is the inverse of how they are presented in the gallery admin panel. To rectify this and make everything nice and user friendly you just need to reverse the get_posts() array that’s returned, before you pass it through the foreach construct.

    So to take your example…

    http://pastebin.com/cJCGAt6e

    Instead of the reverse array code, I believe changing from descending gallery order to ascending is solved more simply by adding this line to the $args array:

    'order' => 'ASC',

    Jeff, what you’re saying really should work but for some reason I can’t get ‘order’ to have any effect. Do you have it working for you?

    Inmo, I am able to get this working although something tripped me up along the way. Maybe you’re having the same situation…

    It never occurred to me that gallery images don’t have a sort order after being uploaded. One must be assigned. In the image light box, on the gallery of images, there is a column named ‘Actions’. When those fields were blank, ‘orderby’ and ‘order’ didn’t affect any change for me no matter what value I assigned to them. Dragging and dropping images or typing values into the ‘Action’ fields – and saving all changes – gave them a value. Then the ‘orderby’ and ‘order’ arguments worked for me in the template code.

    Hope that helps.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Control sort order when using wp_get_attachment_image ?’ is closed to new replies.