自定义帖子类型的自定义列

时间:2018-02-28 03:42:28

标签: wordpress

我在下面有以下代码来创建自定义帖子类型。

function custom_post_type() {
    register_post_type('multi-step-form', array(
    'supports' => array('title'),
    'public' => false,
    'show_ui' => true,
    'labels' => array(
      'name' => 'Multi-Step Forms',
      'add_new_item' => 'Add New Multi-Step Form',
      'edit_item' => 'Edit Multi-Step Form',
      'all_items' => 'All Multi-Step Forms',
      'singular_name' => 'Multi-Step Form'
    ),
    'menu_icon' => 'dashicons-universal-access-alt',
  ));               
}   

add_action( 'init',  'custom_post_type');   

如何在mange post类型屏幕中添加自己的ID或slug等自定义列?

请看截图:

enter image description here

先谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个

add_filter('manage_posts_columns', 'default_columns_head');

function default_columns_head($defaults) {
    global $current_screen;
    if (in_array($current_screen->post_type, array('multi-step-form','other_post_type'))) {
        $defaults['col1'] = 'col1 name';
        $defaults['col2'] = 'col2 name';
    }
    return $defaults;
}
function default_columns_content($column_name, $post_ID) {

    if ($column_name == 'col1') {
        $post = get_post($post_ID);
       echo 'col 1 data';
    } elseif ($column_name == 'col2') {
         $post = get_post($post_ID);
        echo 'col2 data';
    }
}

add_action('manage_posts_custom_column', 'default_columns_content', 10, 2);

答案 1 :(得分:0)

这应仅显示自定义帖子类型的自定义列,而不显示其他帖子类型。

add_filter('manage_multi-step-form_columns', 'my_custom_post_columns');

function my_custom_post_columns($columns) {
    $columns['post-id'] = 'Post ID';
    $columns['post-slug'] = 'Post Slug';

    return $columns;
}

add_action('manage_multi-step-form_custom_column',  'show_my_custom_columns');

function show_my_custom_columns( $name ) {
    global $post;

    switch ($name) {
        case 'post-id':
            echo $post->ID;
            break;
        case 'post-slug':
            echo $post->post_name;
            break;
    }
}