首页 > 解决方案 > 将自定义分类法添加到所有帖子页面

问题描述

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

谢谢

截屏

自定义帖子类型

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

标签: wordpresscustom-post-type

解决方案


请参阅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' );
}

推荐阅读