更改编辑面板的自定义列的顺序


27

当您像这样注册自定义列时:

//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;
}

默认情况下,它显示为右侧的最后一个。如何更改订单?如果我想将以上列显示为第一列或第二列怎么办?

先感谢您

Answers:


36

您基本上是在问一个PHP问题,但是我会回答,因为它是在WordPress的上下文中。您需要重新构建columns数组,将您的列插入要保留的列之前

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;
}

是的,我想那会是更简单的方法:)但我的回答正确。好主意。
Bainternet

בנייתאתרים-当您回答您的答案时,我几乎已经写完了我的答案,因此,可以这么说,我们的答案“在邮件中交叉了”。无论如何,我花了一些时间才弄清楚。当我第一次需要它的时候,我当然没有想到。
MikeSchinkel

需要注意的一件事:如果另一个插件删除了作者专栏,会发生什么?您自己的缩略图列也会消失。您可以isset($new['thumbnail'])在返回之前进行检查$new。如果未设置,则只需将其附加在末尾即可。
Geert 2012年

5

如果您有WPML之类的插件会自动添加列,甚至自动添加到自定义帖子类型,则表标题中可能包含复杂的代码。

您不想将代码复制到列定义中。为什么要有人呢?

我们只想扩展已经提供的,格式良好且可排序的默认列。

实际上,这只是七行代码,它使所有其他列保持不变。

# hook into manage_edit-<mycustomposttype>_columns
add_filter( 'manage_edit-mycustomposttype_columns', 'mycustomposttype_columns_definition' ) ;

# column definition. $columns is the original array from the admin interface for this posttype.
function mycustomposttype_columns_definition( $columns ) {

  # add your column key to the existing columns.
  $columns['mycolumn'] = __( 'Something different' ); 

  # now define a new order. you need to look up the column 
  # names in the HTML of the admin interface HTML of the table header. 
  #   "cb" is the "select all" checkbox.
  #   "title" is the title column.
  #   "date" is the date column.
  #   "icl_translations" comes from a plugin (in this case, WPML).
  # change the order of the names to change the order of the columns.
  $customOrder = array('cb', 'title', 'icl_translations', 'mycolumn', 'date');

  # return a new column array to wordpress.
  # order is the exactly like you set in $customOrder.
  foreach ($customOrder as $colname)
    $new[$colname] = $columns[$colname];    
  return $new;
}

希望这可以帮助..


3

我知道的唯一方法是创建自己的列数组

// 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;
    }

然后像往常一样呈现此额外添加的列

// 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
}

希望这可以帮助


2

这是一些SO答案的组合,希望对您有所帮助!

function array_insert( $array, $index, $insert ) {
    return array_slice( $array, 0, $index, true ) + $insert +
    array_slice( $array, $index, count( $array ) - $index, true);
}

add_filter( 'manage_resource_posts_columns' , function ( $columns ) {
    return array_insert( $columns, 2, [
        'image' => 'Featured Image'
    ] );
});

我发现array_splice()不会像我们需要的那样保留自定义键。array_insert()做。


1
这应该是正确的答案。
xudre '18
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.