11

I have built a custom post type called article and the information given on the admin summary screen is sparse. I was able to add the featured image post thumbnail image using the http://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column from a tutorial.

However I would like to be able to get an overview of the categories and sub categories that these posts have had assigned to them on the admin page. ie adding a column for that part?

Here is the code that I have used to register the taxonomy in the custom post types code

14

The register_taxonomy function has a parameter called show_admin_column that will handle adding a column. Have you tried that?

eg:

register_taxonomy(
    'my_tax, 
    'post_type', 
    array(
        'label'             => 'My Taxonomy',
        'show_admin_column' => true,
        )
);
  • 1
    Please, add code and explain how using it answers the quesiton. If you just want to ask something to the OP, use the comments. – cybmeta Dec 21 '16 at 15:00
6

After some searching, I have found a solution using the manage_edit-${post_type}_columns filter and the manage_${post_type}_posts_custom_column action.

The columns are created with the filter and then the column is populated with the action. I assume additional columns could be added and populated quite easily using the ideas in this link 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;
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.