0

I created a custom post type. I have an overview page with Title, Author, Editor. These are defined in the supports array ('supports' => array( 'title', 'author', 'editor' ),). Is it possible to add some custom values to the overview, e.g. values from custom posts?

BR, mybecks

  • You can use add_meta_box() function in your custom posts. – Balas Dec 17 '13 at 10:02
  • Yes, it is possible. Can you modify your question and provide description about what you exactly need to achieve? – Chittaranjan Dec 17 '13 at 11:07
2

To customize the columns you basically need a combo of the manage_edit-$post-type_columns filter and the `manage_$post-type_posts_custom_column' action hook. The former tells WordPress the names of the columns you'd like to display and the latter provides the HTML to echo out for each particular colunmn... especially custom columns that WordPress has not already defined.

The following example was taken from Custom columns for custom post types by Justin Tadlock and adds custom 'Duration' and 'Genre' columns to a 'Movie' post type.

add_filter( 'manage_edit-movie_columns', 'my_edit_movie_columns' ) ;

function my_edit_movie_columns( $columns ) {

    $columns = array(
        'cb' => '<input type="checkbox" />',
        'title' => __( 'Movie' ),
        'duration' => __( 'Duration' ),
        'genre' => __( 'Genre' ),
        'date' => __( 'Date' )
    );

    return $columns;
}

add_action( 'manage_movie_posts_custom_column', 'my_manage_movie_columns', 10, 2 );

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

    switch( $column ) {

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

            /* Get the post meta. */
            $duration = get_post_meta( $post_id, 'duration', true );

            /* If no duration is found, output a default message. */
            if ( empty( $duration ) )
                echo __( 'Unknown' );

            /* If there is a duration, append 'minutes' to the text string. */
            else
                printf( __( '%s minutes' ), $duration );

            break;

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

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

            /* 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, 'genre' => $term->slug ), 'edit.php' ) ),
                        esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'genre', 'display' ) )
                    );
                }

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

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

            break;

        /* Just break out of the switch statement for everything else. */
        default :
            break;
    }
}

There is also another example in the Codex.

  • this was exactly what I'm looking for. Big thanks! – mybecks Dec 18 '13 at 8:55

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.