【问题标题】:Adding Custom Taxonomies to all posts page将自定义分类法添加到所有帖子页面
【发布时间】:2019-07-11 17:03:11
【问题描述】:

我有一个自定义帖子类型,员工,并且这个帖子类型有一个自定义分类,角色。我希望能够在 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 );

【问题讨论】:

    标签: wordpress custom-post-type


    【解决方案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' );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多