将类别添加到自定义帖子类型的管理列中?


13

我建立了一个名为article的自定义帖子类型,并且“管理摘要”屏幕上给出的信息很少。我可以使用来自教程的http://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column添加特色图片发布缩略图。

但是,我希望能够在管理页面上概述这些帖子已分配给他们的类别和子类别。即为该部分添加一列?

这是我用来在自定义帖子类型代码中注册分类法的代码


Answers:


18

register_taxonomy函数有一个名为参数show_admin_column,将处理添加一列。你有尝试过吗?

例如:

register_taxonomy(
    'my_tax, 
    'post_type', 
    array(
        'label'             => 'My Taxonomy',
        'show_admin_column' => true,
        )
);

1
请添加代码并说明如何使用它回答问题。如果您只想向OP提问,请使用注释。
cybmeta '16

6

经过一些搜索,我找到了使用manage_edit-${post_type}_columns过滤器和manage_${post_type}_posts_custom_column操作的解决方案。

使用过滤器创建列,然后使用操作填充该列。我假设可以使用此链接中的想法轻松添加和填充其他列http://justintadlock.com/archives/2011/06/27/custom-columns-for-custom-post-types

add_filter('manage_edit-article_columns', 'my_columns');
function my_columns($columns) {
    $columns['article_category'] = 'Category';
return $columns;
}

add_action( 'manage_article_posts_custom_column', 'my_manage_article_columns', 10, 2 );

function my_manage_article_columns( $column, $post_id ) {
global $post;

switch( $column ) {

    /* If displaying the 'article_category' column. */
    case 'article_category' :

        /* Get the genres for the post. */
        $terms = get_the_terms( $post_id, 'article_category' );

        /* If terms were found. */
        if ( !empty( $terms ) ) {

            $out = array();

            /* Loop through each term, linking to the 'edit posts' page for the specific term. */
            foreach ( $terms as $term ) {
                $out[] = sprintf( '<a href="%s">%s</a>',
                    esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'article_category' => $term->slug ), 'edit.php' ) ),
                    esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'article_category', 'display' ) )
                );
            }

            /* Join the terms, separating them with a comma. */
            echo join( ', ', $out );
        }

        /* If no terms were found, output a default message. */
        else {
            _e( 'No Articles' );
        }

        break;

    /* Just break out of the switch statement for everything else. */
    default :
        break;
}
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.