具有不同分类的多个自定义邮政类型

时间:2017-05-05 作者:rose102593

我能够创建多个自定义帖子类型,但在注册分类法时遇到了问题。我想创建每个CPT都有自己的分类法,而不是共享的。

使现代化

大家好,谢谢你们回答我的问题。我已经这样做了:

function CPT-1(){
$labels = array(
    \'name\'                => __( \'Careers\' ),
    \'singular_name\'       => __( \'Career\'),
    \'menu_name\'           => __( \'Careers\'),
    \'parent_item_colon\'   => __( \'Parent Careers\'),
    ..etc..
);
$args = array(
    \'label\'               => __( \'career\'),
    \'description\'         => __( \'careers\'),
    ....etc...
);
register_post_type( \'cpt1\', $args );
}

    function CPT2(){
$labels = array(
    \'name\'                => __( \'Careers\' ),
    \'singular_name\'       => __( \'Career\'),
    \'menu_name\'           => __( \'Careers\'),
    ...etc...
);
$args = array(
    \'label\'               => __( \'career\'),
    \'description\'         => __( \'careers\'),
    \'labels\'              => $labels,
    \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', 
  .....etc.....
);
register_post_type( \'cpt2\', $args );
}

function cma_register_taxonomies() {
$taxonomies = array(
    array(
        \'slug\'         => \'department\',
        \'single_name\'  => \'Department\',
        \'plural_name\'  => \'Department\',
        \'post_type\'    => \'cpt1\',
        \'rewrite\'      => array( \'slug\' => \'department\' ),
    ),
    array(
        \'slug\'         => \'job-type\',
        \'single_name\'  => \'Type\',
        \'plural_name\'  => \'Types\',
        \'post_type\'    => \'cp2\',
        \'hierarchical\' => false,
    )
    ),
);
foreach( $taxonomies as $taxonomy ) {
    $labels = array(
        \'name\' => $taxonomy[\'plural_name\'],
        \'singular_name\' => $taxonomy[\'single_name\'],
        \'search_items\' =>  \'Search \' . $taxonomy[\'plural_name\'],
        \'all_items\' => \'All \' . $taxonomy[\'plural_name\'],
        \'parent_item\' => \'Parent \' . $taxonomy[\'single_name\'],
        \'parent_item_colon\' => \'Parent \' . $taxonomy[\'single_name\'] . \':\',
        \'edit_item\' => \'Edit \' . $taxonomy[\'single_name\'],
        \'update_item\' => \'Update \' . $taxonomy[\'single_name\'],
        \'add_new_item\' => \'Add New \' . $taxonomy[\'single_name\'],
        \'new_item_name\' => \'New \' . $taxonomy[\'single_name\'] . \' Name\',
        \'menu_name\' => $taxonomy[\'plural_name\']
    );

    $rewrite = isset( $taxonomy[\'rewrite\'] ) ? $taxonomy[\'rewrite\'] : array( \'slug\' => $taxonomy[\'slug\'] );
    $hierarchical = isset( $taxonomy[\'hierarchical\'] ) ? $taxonomy[\'hierarchical\'] : true;

    register_taxonomy( $taxonomy[\'slug\'], $taxonomy[\'post_type\'], array(
        \'hierarchical\' => $hierarchical,
        \'labels\' => $labels,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => $rewrite,
    ));
}

}

1 个回复
SO网友:Bram van Rijswijk

Try this:

function post_type_categories() {
        $field_args = array(
            \'labels\' => array(
            \'name\'              => \'Categories\', \'taxonomy general name\',
            \'singular_name\'     => \'Category\', \'taxonomy singular name\',
            \'search_items\'      => \'Search categories\',
            \'all_items\'         => \'All categories\',
            \'parent_item\'       => \'Parent category\',
            \'parent_item_colon\' => \'Parent category:\',
            \'edit_item\'         => \'Edit category\',
            \'update_item\'       => \'Edit category\',
            \'add_new_item\'      => \'Add new category\',
            \'new_item_name\'     => \'New category\',
            \'menu_name\'         => \'Categories\',
            ),
            \'hierarchical\' => true
        );
        register_taxonomy( \'post_type_categories\', \'{your_post_type_name}\', $field_args );
    }    

相关推荐

Order by taxonomy Value

我确实想每年订购CTP。我有一个分类法叫做“annee”(年份),还有一个CTP叫做“moto”(摩托车)。问题是,当我试图按分类法排序时,它只按分类法ID排序,而不是按分类法值排序。你有什么解决办法吗?if ( is_post_type_archive( \'motos\' ) ) { $query->set( \'posts_per_archive_page\', 24 ); $query->set( \'order\', \'DESC\' );&