custom field in admin columns

时间:2013-05-11 作者:SoupOfStars

我正在试图了解如何将自定义字段incr\\u编号放入admin列。然而,我无法理解,我在谷歌上搜索,但找不到直接的答案。

这不会那么难吧?

   <?php


   /*-----------------------------------------------------------------------------------*/
   /* WooThemes supportpress Custom Post Types Init */
   /*-----------------------------------------------------------------------------------*/

  function woo_supportpress_post_types_init() {

    add_option(\'tickets_slug\', \'tickets\');

 register_post_type( \'ticket\',
    array(
        \'label\' => __( \'Ticket\', \'woothemes\' ),
        \'labels\' => array(
            \'name\' => __( \'Tickets\', \'woothemes\' ),
            \'singular_name\' => __( \'Ticket\', \'woothemes\' ),
            \'all_items\'   => __( \'All Tickets\', \'woothemes\' ),
            \'add_new\' => __( \'Add New\', \'woothemes\' ),
            \'add_new_item\' => __( \'Add New Ticket\', \'woothemes\' ),
            \'edit\' => __( \'Edit\', \'woothemes\' ),
            \'edit_item\' => __( \'Edit Ticket\', \'woothemes\' ),
            \'new_item\' => __( \'New Ticket\', \'woothemes\' ),
            \'view\' => __( \'View Tickets\', \'woothemes\' ),
            \'view_item\' => __( \'View Ticket\', \'woothemes\' ),
            \'search_items\' => __( \'Search Tickets\', \'woothemes\' ),
            \'not_found\' => __( \'No tickets found\', \'woothemes\' ),
            \'not_found_in_trash\' => __( \'No tickets found in trash\', \'woothemes\' ),
            \'parent\' => __( \'Parent Tickets\', \'woothemes\' ),
        ),
        \'description\' => __( \'This is where you can create new tickets for your site. Tickets can also be created from the front-end.\', \'woothemes\' ),
        \'public\' => true,
        \'menu_position\' => 4,
        \'show_ui\' => true,
        \'capability_type\' => \'post\',
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => true,
        \'hierarchical\' => false,
        \'rewrite\' => array( \'slug\' => get_option(\'tickets_slug\'), \'with_front\' => false ),
        \'query_var\' => true,
        \'has_archive\' => \'tickets\',
        \'supports\' => array( \'title\', \'editor\', \'custom-fields\', \'author\', \'comments\' ),
    )
);



register_taxonomy( \'ticket_status\',
    array(\'ticket\'),
    array(
        \'hierarchical\' => true,
        \'labels\' => array(
            \'name\' => __( \'Ticket Statuses\', \'woothemes\'),
            \'singular_name\' => __( \'Ticket Status\', \'woothemes\'),
            \'search_items\' =>  __( \'Search Ticket Statuses\', \'woothemes\'),
            \'all_items\' => __( \'All Ticket Statuses\', \'woothemes\'),
            \'parent_item\' => __( \'Parent Ticket Status\', \'woothemes\'),
            \'parent_item_colon\' => __( \'Parent Ticket Status:\', \'woothemes\'),
            \'edit_item\' => __( \'Edit Ticket Status\', \'woothemes\'),
            \'update_item\' => __( \'Update Ticket Status\', \'woothemes\'),
            \'add_new_item\' => __( \'Add New Ticket Status\', \'woothemes\'),
            \'new_item_name\' => __( \'New Ticket Status\', \'woothemes\')
        ),
        \'public\' => true,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'ticket-status\', \'with_front\' => false ),
    )
);

register_taxonomy( \'ticket_priority\',
    array(\'ticket\'),
    array(
        \'hierarchical\' => true,
        \'labels\' => array(
                \'name\' => __( \'Ticket Priorities\', \'woothemes\'),
                \'singular_name\' => __( \'Ticket Priority\', \'woothemes\'),
                \'search_items\' =>  __( \'Search Ticket Priorities\', \'woothemes\'),
                \'all_items\' => __( \'All Ticket Priorities\', \'woothemes\'),
                \'parent_item\' => __( \'Parent Ticket Priority\', \'woothemes\'),
                \'parent_item_colon\' => __( \'Parent Ticket Priority:\', \'woothemes\'),
                \'edit_item\' => __( \'Edit Ticket Priority\', \'woothemes\'),
                \'update_item\' => __( \'Update Ticket Priority\', \'woothemes\'),
                \'add_new_item\' => __( \'Add New Ticket Priority\', \'woothemes\'),
                \'new_item_name\' => __( \'New Ticket Priority\', \'woothemes\')
        ),
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'ticket-priority\', \'with_front\' => false ),
    )
);

register_taxonomy( \'ticket_type\',
    array(\'ticket\'),
    array(
        \'hierarchical\' => true,
        \'labels\' => array(
                \'name\' => __( \'Ticket Types\', \'woothemes\'),
                \'singular_name\' => __( \'Ticket Type\', \'woothemes\'),
                \'search_items\' =>  __( \'Search Ticket Types\', \'woothemes\'),
                \'all_items\' => __( \'All Ticket Types\', \'woothemes\'),
                \'parent_item\' => __( \'Parent Ticket Type\', \'woothemes\'),
                \'parent_item_colon\' => __( \'Parent Ticket Type:\', \'woothemes\'),
                \'edit_item\' => __( \'Edit Ticket Type\', \'woothemes\'),
                \'update_item\' => __( \'Update Ticket Type\', \'woothemes\'),
                \'add_new_item\' => __( \'Add New Ticket Type\', \'woothemes\'),
                \'new_item_name\' => __( \'New Ticket Type\', \'woothemes\')
        ),
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'ticket-type\', \'with_front\' => false ),
    )
);

register_taxonomy( \'ticket_tags\',
    array(\'ticket\'),
    array(
        \'hierarchical\' => false,
        \'labels\' => array(
                \'name\' => __( \'Ticket Tags\', \'woothemes\'),
                \'singular_name\' => __( \'Ticket Tags\', \'woothemes\'),
                \'search_items\' =>  __( \'Search Ticket Tags\', \'woothemes\'),
                \'all_items\' => __( \'All Ticket Tags\', \'woothemes\'),
                \'parent_item\' => __( \'Parent Ticket Tag\', \'woothemes\'),
                \'parent_item_colon\' => __( \'Parent Ticket Tag:\', \'woothemes\'),
                \'edit_item\' => __( \'Edit Ticket Tag\', \'woothemes\'),
                \'update_item\' => __( \'Update Ticket Tag\', \'woothemes\'),
                \'add_new_item\' => __( \'Add New Ticket Tag\', \'woothemes\'),
                \'new_item_name\' => __( \'New Ticket Tag\', \'woothemes\')
        ),
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'ticket-tag\' ),
    )
);


  }  

  add_action( \'init\', \'woo_supportpress_post_types_init\', 0 );


 /*-----------------------------------------------------------------------------------*/
 /* Admin columns for post types */
 /*-----------------------------------------------------------------------------------*/

 function woo_supportpress_ticket_columns($old_columns){

$columns = array(

\'cb\' => \'<input type=\\"checkbox\\" />\',
\'title\' => __(\'Ticket Title\', \'woothemes\'),
\'incr_number\' => __(\'Ticket ID\', \'woothemes\'),
\'status\' => __(\'Status\', \'woothemes\'),
\'priority\' => __(\'Priority\', \'woothemes\'),
\'type\' => __(\'Type\', \'woothemes\'),
\'author\' => __(\'Submitted by\', \'woothemes\'),
\'assigned\' => __(\'Assigned to\', \'woothemes\'),
\'comments\' => $old_columns["comments"],
\'date\' => __(\'Date\', \'woothemes\'),
);

return $columns;

 }

add_action( \'custom_woo_supportpress_ticket_columns\',    \'custom_woo_supportpress_ticket_columns\', 10, 2 );

function custom_woo_supportpress_ticket_columns( $column, $post_id ) {
global $post;

switch( $column ) {

    /* If displaying the \'incr_number\' column. */
    case \'incr_number\' :

        /* Get the post meta. */
        $incr_number = get_post_meta($post_id, \'incr_number\', true );



        break;

}
 }

 add_filter(\'manage_edit-ticket_columns\', \'woo_supportpress_ticket_columns\');

 function woo_supportpress_ticket_custom_columns($column) {
global $post;

$ticket_details = woo_supportpress_get_ticket_details( $post->ID );

switch ($column) {
    case "status" :
        echo get_the_term_list($post->ID, \'ticket_status\', \'\', \', \',\'\');
    break;
    case "priority" :
        echo get_the_term_list($post->ID, \'ticket_priority\', \'\', \', \',\'\');
    break;
    case "type" :
        echo get_the_term_list($post->ID, \'ticket_type\', \'\', \', \',\'\');
    break;
    case "assigned" :

        if ($ticket_details[\'assigned_to\']->ID>0) :
            $link = get_author_posts_url( $ticket_details[\'assigned_to\']->ID );
        else :
            $link = add_query_arg(\'assigned_to\', \'0\', get_post_type_archive_link(\'ticket\'));
        endif;

        echo \'<a href="\'.$link.\'">\'.$ticket_details[\'assigned_to\']->display_name.\'</a>\';

    break;
    case "id" :
        echo \'#\'.$post->ID;
    break;
}
 }
add_action(\'manage_ticket_posts_custom_column\', \'woo_supportpress_ticket_custom_columns\', 2);

1 个回复
最合适的回答,由SO网友:Simon Blackbourn 整理而成

试着用这个替换从评论向下的所有内容。基本上,您似乎有一个没有被调用的函数,您需要添加case "incr_number" 测试到woo_supportpress_ticket_custom_columns 功能:

/*-----------------------------------------------------------------------------------*/
/* Admin columns for post types */
/*-----------------------------------------------------------------------------------*/

function woo_supportpress_ticket_columns( $old_columns ){

    $columns = array(
        \'cb\'          => \'<input type=\\"checkbox\\" />\',
        \'title\'       => __(\'Ticket Title\', \'woothemes\'),
        \'incr_number\' => __(\'Ticket ID\', \'woothemes\'),
        \'status\'      => __(\'Status\', \'woothemes\'),
        \'priority\'    => __(\'Priority\', \'woothemes\'),
        \'type\'        => __(\'Type\', \'woothemes\'),
        \'author\'      => __(\'Submitted by\', \'woothemes\'),
        \'assigned\'    => __(\'Assigned to\', \'woothemes\'),
        \'comments\'    => $old_columns["comments"],
        \'date\'        => __(\'Date\', \'woothemes\'),
        );

    return $columns;

}

add_filter(\'manage_edit-ticket_columns\', \'woo_supportpress_ticket_columns\');

function woo_supportpress_ticket_custom_columns( $column ) {

    global $post;

    $ticket_details = woo_supportpress_get_ticket_details( $post->ID );

    switch ($column) {
        case "incr_number" :
            echo get_post_meta( $post->ID, \'incr_number\', true );
        break;
        case "status" :
            echo get_the_term_list($post->ID, \'ticket_status\', \'\', \', \',\'\');
        break;
        case "priority" :
            echo get_the_term_list($post->ID, \'ticket_priority\', \'\', \', \',\'\');
        break;
        case "type" :
            echo get_the_term_list($post->ID, \'ticket_type\', \'\', \', \',\'\');
        break;
        case "assigned" :
            if ($ticket_details[\'assigned_to\']->ID>0) :
                $link = get_author_posts_url( $ticket_details[\'assigned_to\']->ID );
            else :
                $link = add_query_arg(\'assigned_to\', \'0\', get_post_type_archive_link(\'ticket\'));
            endif;
            echo \'<a href="\'.$link.\'">\'.$ticket_details[\'assigned_to\']->display_name.\'</a>\';
        break;
        case "id" :
            echo \'#\'.$post->ID;
        break;
    }
}

add_action(\'manage_ticket_posts_custom_column\', \'woo_supportpress_ticket_custom_columns\', 2);

结束

相关推荐

仅在phpMyAdmin中使用ID创建数百个用户

我也在努力this post. 但我对将记录插入包含要导入的成员的所有ID的wp\\U users表中现在,我有3个成员,分别ID为1、11和12。但我需要插入500个用户,ID从2到502。如何在数据库中创建这些ID行而不逐个创建?现在,我知道如何一次添加一个,但它从id 510开始(因为我删除了较老的用户)。我想从ID 2开始。。。非常感谢!塞德里克