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

kxeu7u2r  于 2023-02-21  发布在  WordPress
关注(0)|答案(4)|浏览(252)

我创建了一个名为横幅的自定义帖子类型。因此,我注册了一个名为位置的新分类,指定横幅显示在哪个页面上。一切都很好,但当我点击管理窗口中的自定义帖子类型'横幅'时,我看到所有创建的横幅,但表中没有分类'位置'的列。
换句话说,我希望能够看到什么位置的横幅是在,在横幅列表。

flvtvl50

flvtvl501#

对于那些感兴趣的人,register_taxonomyregister_taxonomy函数,从WordPress 3.5开始,现在提供了show_admin_column的参数(默认为false),设置为true,它会自动显示admin中的taxonomy列。

l7wslrjt

l7wslrjt2#

您可以使用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>';
}
eyh26e7m

eyh26e7m3#

//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 );
 }
shstlldc

shstlldc4#

除了Jonathan Wold的答案之外,您还可以添加按自定义分类法进行过滤的功能,如下所示:

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>';
    }
}

相关问题