Showing custom taxonomy column in custom posts type listings

前端 未结 4 1877
深忆病人
深忆病人 2021-02-13 06:44

I have created a custom post type named banners. Thereby I register a new taxonomy called location that specifies on which page the banner is t

4条回答
  •  深忆病人
    2021-02-13 07:30

    You can use the manage_post-type_custom_column and manage_edit_post-type_columns filters to add your taxonomy column to the post type listing.

    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[] =" " .esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "";
                echo join('', $post_terms );
            }
             else echo 'No Location Set. ';
    }
    

提交回复
热议问题