Showing custom taxonomy column in custom posts type listings

前端 未结 4 1874
深忆病人
深忆病人 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:17

    For those interested, the register_taxonomy function, as of WordPress 3.5, now offers an argument for show_admin_column (false by default). Set to true, it automatically displays the taxonomy column in the admin.

    0 讨论(0)
  • 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[] ="<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>';
    }
    
    0 讨论(0)
  • 2021-02-13 07:30
     //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 );
     }
    
    0 讨论(0)
  • 2021-02-13 07:36

    In addition to Jonathan Wold answer, you can add the ability to filter by your custom taxonomy like this:

    add_action('restrict_manage_posts', 'filter_post_type_backend_by_taxonomies', 99, 2);
    
    function filter_post_type_backend_by_taxonomies($post_type, $which) {
        if( 'your_post_type' !== $post_type) {
            return;
        }
        
        $taxonomies = array( 'your_custom_taxonomy' );
        
        foreach( $taxonomies as $taxonomy_slug ) {
            $taxonomy_obj = get_taxonomy( $taxonomy_slug );
            $taxonomy_name = $taxonomy_obj->labels->name;
            
            $terms = get_terms($taxonomy_slug);
            
            echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
            echo '<option value="">' . sprintf( esc_html__( 'Show all %s', 'domain-name' ), $taxonomy_name) . '</option>';
            foreach ( $terms as $term ) {
                printf(
                        '<option value="%1$s" %2$s>%3$s (%4$s)</option>',
                        $term->slug,
                        ( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
                        $term->name,
                        $term->count
                );
                }
            echo '</select>';
        }
    }
    
    0 讨论(0)
提交回复
热议问题