dongtuhe0506 2013-08-02 16:31
浏览 27

wordpress将自定义列添加到自定义帖子类型

I am currently working with WooCommerce, a eCommerce plugin for WordPress. WooCommerce products get setup in a new post-type called product. I have the following code which adds a custom column to the edit screen for this post type:

add_filter( 'manage_edit-product_columns', 'my_edit_product_columns' ) ;
function my_edit_product_columns( $columns ) {
$columns = array(
    'myfield' => __( 'My field' )
);
return $columns;
}

This works fine, but unfortunatley it add's it as the last column. Is there a way to order the columns? I would like this column to be directly after the "Price Column"

  • 写回答

2条回答 默认 最新

  • douqulv6059 2013-08-02 17:28
    关注

    $columns that is being passed to your function my_edit_product_columns is an array of all of the existing columns. You can replace the entire thing or use any standard array manipulation to change the columns & column order.

    For example, if you wanted to specify the columns you would do something like (taken from an events custom post type I use):

    $columns = array(  
                    "cb" => "<input type=\"checkbox\" />",
                    "title" => "Event Name",  
                    "event_date" => "Date",   
                    "start_time"=>"Time",
                 );  
    

    So if you simply print_r($columns) to see what it currently has, you could manually reorder it.

    To insert your column at a specific position in the existing $columns array use:

    # Insert at offset 2
    $offset = 2;
    $newArray = array_slice($columns, 0, $offset, true) +
            array('new_column_id' => 'New Column Name') +
            array_slice($columns, $offset, NULL, true);
    

    See this thread for more on that: array_splice() for associative arrays

    //ADDED

    I just tested this out locally on a custom post type I have in use called products. This code works fine for me. The offset starts with column 1, so to make my new column be the second one, I set offset at 2.

       public function productsListColumns($columns){
        $columns = array(  
                    "cb" => "<input type=\"checkbox\" />",
                    "title" => "Product",  
                    "price" => "Price"
                 );  
                $offset = 2;
                $newArray = array_slice($columns, 0, $offset, true) +
                array('new_column_id' => 'New Column Name') +
                array_slice($columns, $offset, NULL, true);
        return $newArray;  
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题