【问题标题】:wordpress adding custom columns to custom post typewordpress 将自定义列添加到自定义帖子类型
【发布时间】:2013-08-04 00:32:02
【问题描述】:

我目前正在使用 WooCommerce,这是一个 WordPress 的电子商务插件。 WooCommerce 产品在称为产品的新帖子类型中进行设置。我有以下代码在此帖子类型的编辑屏幕中添加了一个自定义列:

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

这很好用,但不幸的是它添加了它作为最后一列。有没有办法对列进行排序?我希望此列直接位于“价格列”之后

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    $columns 传递给您的函数 my_edit_product_columns 是所有现有列的数组。您可以替换整个内容或使用任何标准数组操作来更改列和列顺序。

    例如,如果您想指定列,您可以执行以下操作(取自我使用的事件自定义帖子类型):

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

    因此,如果您只是简单地 print_r($columns) 来查看它当前的内容,您可以手动对其进行重新排序。

    要将列插入现有 $columns 数组中的特定位置,请使用:

    # 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);
    

    有关更多信息,请参阅此主题:array_splice() for associative arrays

    //添加

    我刚刚在我使用的名为 products 的自定义帖子类型上进行了本地测试。这段代码对我来说很好。偏移量从第 1 列开始,所以为了使我的新列成为第二列,我将偏移量设置为 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;  
    }
    

    【讨论】:

    • 感谢您的回答,我已经尝试了 array_slice 并且该列仍然是最后一个?
    • 非常酷的答案 - 比尝试用 jQuery 破解这个要好得多
    【解决方案2】:

    请检查以下代码。您只需要将您的帖子类型替换为 TribeEvents::POSTTYPE

      add_filter('manage_' . TribeEvents::POSTTYPE . '_posts_columns', 'column_headers');
    add_action( 'manage_posts_custom_column', 'custom_columns' , 10, 2 );
    
    function column_headers($columns)
    {
        $columns['tickets-attendees']   = __( 'Attendees', 'tribe-events-calendar');
        return $columns;
    }
    function custom_columns( $column_id, $post_id ) {
    
        if ( $column_id == 'tickets-attendees' ) {
             //echo $post_id;
             $items = TribeEventsTickets::get_event_attendees($post_id );
             $attendeeCount = count($items);
             ?>
             <a href = "edit.php?post_type=tribe_events&page=tickets-attendees&event_id=<?php echo $post_id; ?>"><?php echo $attendeeCount; ?></a>
    
             <?php 
    
        }
    }      
    

    【讨论】:

      猜你喜欢
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      相关资源
      最近更新 更多