将自定义分类法添加到所有帖子页面

时间:2019-07-11 17:03:11

标签: wordpress custom-post-type

我有一个自定义帖子类型,Staff,此帖子类型有一个自定义分类法,角色。我希望能够在wp-admin后端的所有人员页面上按/排序角色,例如默认情况下帖子的类别和标签如何工作。

谢谢

screenshot

自定义帖子类型

function register_staff(){
    $labels = array(
        'name' => 'Staff',
        'singular_name' => 'Staff',
        'add_new' => 'Add Staff',
        'all_items' => 'All Staff',
        'add_new_item' => 'Add Staff Member',
        'edit_item' => 'Edit Staff Member',
        'new_item' => 'New Staff Member',
        'view_item' => 'View staff',
        'search_item' => 'Search staff',
        'not_found' => 'No Items Found',
        'not_found_in_trash' => 'No staff found in trash',
    );
    $args = array(
        'labels' => $labels,
        'public' => false,
    'show_ui' => true,
        'has_index' => true,
        'has_archive' => true,
        'publicly_queryable' => true,
        'query_var' => true,
        'has_archive' => true,
    'rewrite' => true,
        'capability_type' => "post",
        'hierarchical' => false,
        'supports' => array(
            'title',
            'editor',
            'page-attributes',
            'excerpt',
            'thumbnail',
            'revisions',
        ),
        'taxonomies' => array("role"),
        'menu_position' => 5,
        'menu_icon' => "dashicons-businessperson",
        'exclude_from_search' => false,
    );
    register_post_type('staff', $args);
}

add_action( "init", "register_staff");

自定义分类

add_action( "init", "register_staff");

function build_taxonomies() {
  register_taxonomy('role', 'staff', array(
    'label' => 'Roles',
    'public' => true,
  ));
}
add_action( 'init', 'build_taxonomies', 0 );

1 个答案:

答案 0 :(得分:1)

请参见https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column

未经测试,就像这样:

if ( !function_exists('AddTaxColumn') ) { 

    function AddTaxColumn($cols) { 

    $cols['yourtaxonomy'] = __('My Taxonomy'); 
    return $cols; 

    } 

    function AddTaxValue($column_name, $post_id) { 

        if ( 'yourtaxonomy' == $column_name ) { 

            $tax_id = wp_get_post_terms( $post_id, 'yourtaxonomy' );


            if ($tax_id) {

                $taxonomies = join( ", ", $tax_id );

                echo $taxonomies; 

            }
        }
    }

add_filter( 'manage_team_posts_columns', 'AddTaxColumn' ); 
add_action( 'manage_team_posts_custom_column', 'AddTaxValue', 10, 2 ); 
add_filter( 'manage_edit-team_sortable_columns', 'AddTaxColumn' );
}