在自定义帖子类型列表中显示自定义分类列

Sar*_*pta 11 wordpress

我创建了一个名为banners的自定义帖子类型.因此,我注册了一个名为location的新分类,指定了要在哪个页面上显示横幅.一切都很棒然而当我点击管理窗口中的自定义帖子类型"横幅"时,我看到所有横幅已创建,但表格没有分类"位置"的列.

换句话说,我希望能够在横幅列表中查看横幅所在的位置.

Jon*_*old 28

对于那些感兴趣的人,自WordPress 3.5起,register_taxonomy函数现在提供了一个参数(默认为false).设置为,它会自动显示admin中的分类列.show_admin_columntrue

  • 这是我一直在寻找的解决方案,还有很多更复杂的解决方案! (2认同)

Chr*_*s_O 7

您可以使用manage_post-type_custom_column和manage_edit_post-type_columns过滤器将分类列添加到帖子类型列表中.

add_action( 'admin_init', 'my_admin_init' );
function my_admin_init() {
    add_filter( 'manage_edit-banner_columns', 'my_new_custom_post_column');
    add_action( 'manage_banner_custom_column', 'location_tax_column_info', 10, 2);
}

function my_new_custom_post_column( $column ) {
    $column['location'] = 'Location';

    return $column;
}

function location_tax_column_info( $column_name, $post_id ) {
        $taxonomy = $column_name;
        $post_type = get_post_type($post_id);
        $terms = get_the_terms($post_id, $taxonomy);

        if (!empty($terms) ) {
            foreach ( $terms as $term )
            $post_terms[] ="<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " .esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>";
            echo join('', $post_terms );
        }
         else echo '<i>No Location Set. </i>';
}
Run Code Online (Sandbox Code Playgroud)


ash*_*med 7

 //what version of wordpress you are using
 //since wp 3.5 you can pass parameter show_admin_column=>true
 // hook into the init action and call create_book_taxonomies when it fires
 add_action( 'init', 'create_book_taxonomies', 0 );

 // create two taxonomies, genres and writers for the post type "book"
 function create_book_taxonomies() {
 // Add new taxonomy, make it hierarchical (like categories)
$labels = array(
    'name'              => _x( 'Genres', 'taxonomy general name' ),
    'singular_name'     => _x( 'Genre', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Genres' ),
    'all_items'         => __( 'All Genres' ),
    'parent_item'       => __( 'Parent Genre' ),
    'parent_item_colon' => __( 'Parent Genre:' ),
    'edit_item'         => __( 'Edit Genre' ),
    'update_item'       => __( 'Update Genre' ),
    'add_new_item'      => __( 'Add New Genre' ),
    'new_item_name'     => __( 'New Genre Name' ),
    'menu_name'         => __( 'Genre' ),
);

$args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array( 'slug' => 'genre' ),
);

register_taxonomy( 'genre', array( 'book' ), $args );
 }
Run Code Online (Sandbox Code Playgroud)