自定义分类未显示在管理中

时间:2014-10-16 作者:timmyg

我制作了一个自定义内容类型和分类的插件。CPT表现良好,但分类学却不好。有很多代码,但分类信息分析如下:

完整代码可在此处找到github

    <?php

function check_exhibition_type_tax() {
        $labels = array(
            \'name\'                  => _x( \'Exhibition Types\', \'taxonomy general name\' ),
            \'singular_name\'         => _x( \'Exhibition Type\', \'taxonomy singular name\' ),
            \'add_new\'               => _x( \'Add New Exhibition Type\', \'Exhibition Type\'),
            \'add_new_item\'          => __( \'Add New Exhibition Type\' ),
            \'edit_item\'             => __( \'Edit Exhibition Type\' ),
            \'new_item\'              => __( \'New Exhibition Type\' ),
            \'view_item\'             => __( \'View Exhibition Type\' ),
            \'search_items\'          => __( \'Search Exhibition Types\' ),
            \'not_found\'             => __( \'No Exhibition Type found\' ),
            \'not_found_in_trash\'    => __( \'No Exhibition Type found in Trash\' ),
        );

        $pages = array(\'ksasexhibits\');

        $args = array(
            \'labels\'            => $labels,
            \'singular_label\'    => __(\'Exhibiton Type\'),
            \'public\'            => true,
            \'show_ui\'           => true,
            \'hierarchical\'      => true,
            \'show_tagcloud\'     => false,
            \'show_in_nav_menus\' => false,
            \'rewrite\'           => array(\'slug\' => \'exhibiton\', \'with_front\' => false ),
         );
        register_taxonomy(\'exhibition_type\', $pages, $args);
}

    add_action(\'init\', \'register_exhibition_type_tax\');

function check_exhibition_type_terms(){

    //see if we already have populated any terms
    $term = get_terms (\'exhibition_type\', array( \'hide_empty\' => false ) );

    //if no terms then lets add our terms
      if( empty( $term ) ){
        $terms = define_exhibition_type_terms();
        foreach( $terms as $term ){
            if( !term_exists( $term[\'name\'], \'exhibition_type\' ) ){
                wp_insert_term( $term[\'name\'], \'exhibition_type\', array( \'slug\' => $term[\'slug\'] ) );
            }
        }
    }

}

add_action ( \'init\', \'check_exhibition_type_terms\' );

function define_exhibiton_type_terms(){

$terms = array(
        \'0\' => array( \'name\' => \'Campus Partnerships\',\'slug\' => \'campus\'),
        \'1\' => array( \'name\' => \'Community Partnerships\',\'slug\' => \'community\'),
        \'2\' => array( \'name\' => \'Independent Study\',\'slug\' => \'independent\'),
        \'3\' => array( \'name\' => \'Digital Work.\',\'slug\' => \'digital\'),
        \'4\' => array( \'name\' => \'Mellon Foundation\',\'slug\' => \'mellon\'),
    );

    return $terms;
}

add_filter( \'manage_edit-ksasexhibits_columns\', \'my_ksasexhibits_columns\' ) ;

function my_ksasexhibits_columns( $columns ) {

    $columns = array(
        \'cb\' => \'<input type="checkbox" />\',
        \'title\' => __( \'Name\' ),
        \'exhibitons\' => __( \'Exhibiton Type\' ),
        \'date\' => __( \'Date\' ),
    );

    return $columns;
}

add_action( \'manage_studyfields_posts_custom_column\', \'my_manage_ksasexhibits_columns\', 10, 2 );

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

    switch( $column ) {

        /* If displaying the \'program_type\' column. */

        case \'exhibitons\' :

            /* Get the program_types for the post. */
            $terms = get_the_terms( $post_id, \'exhibition_type\' );

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

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

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

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

?>

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

您注册分类法的方式似乎有问题。我宁愿先注册分类法,然后再注册post类型。Exhibit Types

我提交了一个拉拽请求,但你看:

<?php

// hook into the init action and call create_book_taxonomies when it fires
add_action( \'init\', \'create_ksasexhibits_taxonomies\', 0 );

// create two taxonomies, genres and writers for the post type "book"
function create_ksasexhibits_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
            \'name\'                  => _x( \'Exhibition Types\', \'taxonomy general name\' ),
            \'singular_name\'         => _x( \'Exhibition Type\', \'taxonomy singular name\' ),
            \'add_new\'               => _x( \'Add New Exhibition Type\', \'Exhibition Type\'),
            \'add_new_item\'          => __( \'Add New Exhibition Type\' ),
            \'edit_item\'             => __( \'Edit Exhibition Type\' ),
            \'new_item\'              => __( \'New Exhibition Type\' ),
            \'view_item\'             => __( \'View Exhibition Type\' ),
            \'search_items\'          => __( \'Search Exhibition Types\' ),
            \'not_found\'             => __( \'No Exhibition Type found\' ),
            \'not_found_in_trash\'    => __( \'No Exhibition Type found in Trash\' ),
        );

        $args = array(
            \'labels\'            => $labels,
            \'singular_label\'    => __(\'Exhibiton Type\'),
            \'public\'            => true,
            \'show_ui\'           => true,
            \'hierarchical\'      => true,
            \'show_tagcloud\'     => false,
            \'show_in_nav_menus\' => false,
            \'rewrite\'           => array(\'slug\' => \'exhibiton\', \'with_front\' => false ),
         );
    register_taxonomy( \'exhibition_type\', \'ksasexhibits\', $args );
}

function register_ksasexhibits_posttype() {
    $labels = array(
            \'name\'              => _x( \'Exhibits & Programs\', \'post type general name\' ),
            \'singular_name\'     => _x( \'Exhibit\', \'post type singular name\' ),
            \'add_new\'           => __( \'Add New Exhibit & Program\' ),
            \'add_new_item\'      => __( \'Add New Exhibit & Program\' ),
            \'edit_item\'         => __( \'Edit Exhibit & Program\' ),
            \'new_item\'          => __( \'New Exhibit & Program\' ),
            \'view_item\'         => __( \'View Exhibit & Program\' ),
            \'search_items\'      => __( \'Search Exhibit & Program\' ),
            \'not_found\'         => __( \'No Exhibit & Program found\' ),
            \'not_found_in_trash\'=> __( \'No Exhibit & Program found in Trash\' ),
            \'parent_item_colon\' => __( \'\' ),
            \'menu_name\'         => __( \'Exhibits & Programs\' )
        );

        //$taxonomies = array( \'exhibition_type\' );

        $supports = array(\'title\',\'revisions\',\'thumbnail\' );

        $post_type_args = array(
            \'labels\'            => $labels,
            \'singular_label\'    => __(\'Exhibit & Program\'),
            \'public\'            => true,
            \'show_ui\'           => true,
            \'publicly_queryable\'=> true,
            \'query_var\'         => true,
            \'capability_type\'   => \'post\',
            \'has_archive\'       => false,
            \'hierarchical\'      => true,
            \'rewrite\'           => array(\'slug\' => \'exhibitons\', \'with_front\' => false ),
            \'supports\'          => $supports,
            \'menu_position\'     => 5,
            //\'taxonomies\'      => $taxonomies,
            \'show_in_nav_menus\' => true
         );
    register_post_type(\'ksasexhibits\',$post_type_args);
}
add_action(\'init\', \'register_ksasexhibits_posttype\');

function define_exhibiton_type_terms() {
    $terms = array(
        \'0\' => array( \'name\' => \'Campus Partnerships\',\'slug\' => \'campus\'),
        \'1\' => array( \'name\' => \'Community Partnerships\',\'slug\' => \'community\'),
        \'2\' => array( \'name\' => \'Independent Study\',\'slug\' => \'independent\'),
        \'3\' => array( \'name\' => \'Digital Work.\',\'slug\' => \'digital\'),
        \'4\' => array( \'name\' => \'Mellon Foundation\',\'slug\' => \'mellon\'),
        );
    return $terms;
}

function check_exhibition_type_terms(){

    //see if we already have populated any terms
    $terms = get_terms (\'exhibition_type\', array( \'hide_empty\' => false ) );

    //if no terms then lets add our terms
    if( empty( $terms ) ){
    $terms = array(
        \'0\' => array( \'name\' => \'Campus Partnerships\',\'slug\' => \'campus\'),
        \'1\' => array( \'name\' => \'Community Partnerships\',\'slug\' => \'community\'),
        \'2\' => array( \'name\' => \'Independent Study\',\'slug\' => \'independent\'),
        \'3\' => array( \'name\' => \'Digital Work.\',\'slug\' => \'digital\'),
        \'4\' => array( \'name\' => \'Mellon Foundation\',\'slug\' => \'mellon\'),
        );
        foreach( $terms as $term ){
            if( !term_exists( $term[\'name\'], \'exhibition_type\' ) ){
                wp_insert_term( $term[\'name\'], \'exhibition_type\', array( \'slug\' => $term[\'slug\'] ) );
            }
        }
    }

}

add_action ( \'init\', \'check_exhibition_type_terms\' );

?>

结束

相关推荐

获取Term_id-Taxonomy元数据插件

我一直在到处找,但找不到任何方法来实现这一目标。我正在使用Taxonomy Metadata plugin 我正在尝试显示创建的自定义元数据。为此,我使用:get_term_meta($term_id, \'my_metadata\', TRUE);. 问题是我找不到获取term\\u id的方法。我在循环中的模板页中。以下是我尝试过的:$term = get_term_by(\'name\', \'name\', \'custom_taxonomy\'); $termid = $term->