WordPress.org

Ready to get started?Download WordPress

Forums

Better sort function for photos in album/gallery (5 posts)

  1. Hannes
    Member
    Posted 5 years ago #

    I recently changed my photo gallery from the Gallery software (http://gallery.menalto.com/) to the inbuilt WordPress gallery feature.

    The WordPress media manager looks great and is pretty user friendly. But there is one thing missing that Gallery had - a good sorting function. In Gallery you could sort my file name, EXIF creation date, etc.

    It would help me a lot of WordPress would have these sorting functions - although you can drag the photos up and down it can be very time consuming if you have 200 photos in one album.

    It would also be nice to be able to reverse the current order with a click of a button.

  2. yoshi
    Member
    Posted 5 years ago #

    You can set orderby and order option to [gallery] shortcode like:

    // Sort by post title(=file name), in ascending order
    [gallery orderby="title" order="ASC"]

    For other options you can use, see get_post's $orderby parameter section([gallery] internally calls this function).

  3. Hannes
    Member
    Posted 5 years ago #

    Yeah, I tried the orderby property but when you click on a thumbnail to view the photo the order is not the same as in the "album view" - you can't really use the previous/next thumbnail links.

  4. yoshi
    Member
    Posted 5 years ago #

    There is a plugin I wrote before. This changes the prev/next link to the same order as album view.
    Still need some more work to make this better but you can give a try if you like:

    <?php
    /*
    Plugin Name: Image sort
    Plugin URI:
    Description: Fix prev/next image order as set in [gallery]
    Author: yoshi
    Version: 0.1
    Author URI:
    */
    
    function imgsort_previous_image_link() {
      imgsort_adjacent_image_link(true);
    }
    
    function imgsort_next_image_link() {
      imgsort_adjacent_image_link(false);
    }
    
    function imgsort_adjacent_image_link($prev = true) {
      global $post;
    
      global $imgsort_order, $imgsort_orderby;
    
      if ($imgsort_orderby == '' && isset($_GET['imgsort_orderby'])) {
        $imgsort_orderby = sanitize_sql_orderby( $_GET['imgsort_orderby'] );
      }
    
      if ($imgsort_order == '' && isset($_GET['imgsort_order'])) {
        $imgsort_order = sanitize_sql_orderby( $_GET['imgsort_order'] );
      }
    
      $orderby = $imgsort_orderby ? $imgsort_orderby : 'menu_order ID';
      $order = $imgsort_order ? $imgsort_order : 'ASC';
    
      $post = get_post($post);
      $attachments = array_values(get_children( array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ));
    
      foreach ( $attachments as $k => $attachment )
        if ( $attachment->ID == $post->ID )
          break;
    
      $k = $prev ? $k - 1 : $k + 1;
    
      if ( isset($attachments[$k]) )
        echo wp_get_attachment_link($attachments[$k]->ID, 'thumbnail', true);
    }
    
    add_filter('attachment_link', 'imgsort_attachment_link', 10, 2);
    
    function imgsort_attachment_link ($link, $id) {
    
      global $imgsort_order, $imgsort_orderby;
    
      if ($imgsort_order)
        $link .= (strpos($link, '?') ? '&' : '?') . "imgsort_order=$imgsort_order";
      if ($imgsort_orderby)
        $link .= (strpos($link, '?') ? '&' : '?') . "imgsort_orderby=$imgsort_orderby";
    
      return $link;
    }
    
    add_filter('post_gallery', 'imgsort_gallery_set_order', 10, 2);
    
    function imgsort_gallery_set_order ($content, $attr) {
      global $imgsort_order, $imgsort_orderby;
    
      if ( isset( $attr['orderby'] ) )
        $imgsort_orderby = sanitize_sql_orderby( $attr['orderby'] );
      if ( isset( $attr['order'] ) )
        $imgsort_order = sanitize_sql_order( $attr['order'] );
    
      return;
    }

    After activating this, you need to edit your image.php template and replace <?php previous_image_link() ?> with <?php imgsort_previous_image_link() ?>, and <?php next_image_link() ?> with <?php imgsort_next_image_link() ?>.

  5. Ovidiu
    Member
    Posted 5 years ago #

    is this still an issue with the stable wp 2.7 version?

Topic Closed

This topic has been closed to new replies.

About this Topic