自定义帖子中的自定义帖子值概述

时间:2013-12-17 作者:mybecks

我创建了一个自定义帖子类型。我有一个标题,作者,编辑概述页。这些在supports数组中定义(\'supports\' => array( \'title\', \'author\', \'editor\' ),). 是否可以在概述中添加一些自定义值,例如自定义帖子中的值?

比尔,麦贝克

1 个回复
最合适的回答,由SO网友:helgatheviking 整理而成

要自定义列,基本上需要manage_edit-$post-type_columns 筛选器和“manage\\u$post-type\\u posts\\u custom\\u column”操作挂钩。前者告诉WordPress您要显示的列的名称,后者提供HTML以回应每个特定列。。。尤其是WordPress尚未定义的自定义列。

以下示例摘自Custom columns for custom post types by Justin Tadlock 并将自定义的“持续时间”和“流派”列添加到“电影”帖子类型中。

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;
    }
}
还有一个example in the Codex.

结束

相关推荐