Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When you register a custom column like so:

//Register thumbnail column for au-gallery type
add_filter('manage_edit-au-gallery_columns', 'thumbnail_column');
function thumbnail_column($columns) {
$columns['thumbnail'] = 'Thumbnail';
return $columns;
}

by default it appears as the last one on the right. How can I change the order? What if I want to show the above column as the first one or the second one?

Thank you in advance

share|improve this question

2 Answers

up vote 6 down vote accepted

Hi @Mirko:

You are basically asking a PHP question, but I'll answer it because it's in the context of WordPress. You basically need to rebuild the columns array, inserting your column before the column you want it to be left of:

add_filter('manage_posts_columns', 'thumbnail_column');
function thumbnail_column($columns) {
  $new = array();
  foreach($columns as $key => $title) {
    if ($key=='author') // Put the Thumbnail column before the Author column
      $new['thumbnail'] = 'Thumbnail';
    $new[$key] = $title;
  }
  return $new;
}
share|improve this answer
Short and clear, thank you! – Mirko Feb 3 '11 at 11:34
@Mirko: Thanks! You are very welcome. – MikeSchinkel Feb 3 '11 at 11:35
yea i guess that would be and easier way :) but i got the idea right in my answer. nice thinking. – Bainternet Feb 3 '11 at 11:47
בניית אתרים - I was almost finished writing my answer when you answered yours, so our answers "crossed in the mail", so to speak. Anyway, it took me a while to figure that out; it certainly didn't occur to me the first time I needed it. – MikeSchinkel Feb 3 '11 at 11:59
One thing to watch out for: what happens if another plugin removed the author column? Your own thumbnail column would disappear also. You could do an isset($new['thumbnail']) check before returning $new. If it isn't set, just append it at the end, for example. – Geert Nov 3 '12 at 19:47

the only way i know how is to create your own array of columns

// Add to admin_init function
add_filter('manage_edit-au-gallery_columns', 'add_my_gallery_columns');

function add_my_gallery_columns($gallery_columns) {
        $new_columns['cb'] = '<input type="checkbox" />';

        $new_columns['id'] = __('ID');
        $new_columns['title'] = _x('Gallery Name', 'column name');
                // your new column somewhere good in the middle
        $new_columns['thumbnail'] = __('Thumbnail');

        $new_columns['categories'] = __('Categories');
        $new_columns['tags'] = __('Tags');
        $new_columns['date'] = _x('Date', 'column name');

        return $new_columns;
    }

and then render this extra added columns like you normally would

// Add to admin_init function
    add_action('manage_au-gallery_posts_custom_column', 'manage_gallery_columns', 10, 2);

    function manage_gallery_columns($column_name, $id) {
        global $wpdb;
        switch ($column_name) {
        case 'id':
            echo $id;
                break;

        case 'Thumbnail':
            $thumbnail_id = get_post_meta( $id, '_thumbnail_id', true );
                // image from gallery
                $attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
                if ($thumbnail_id)
                    $thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
                elseif ($attachments) {
                    foreach ( $attachments as $attachment_id => $attachment ) {
                        $thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
                    }
                }
                if ( isset($thumb) && $thumb ) {echo $thumb; } else {echo __('None');}
            break;
        default:
            break;
        } // end switch
}

Hope This Helps

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.