将标准WordPress类别与CPT一起使用

时间:2013-09-19 作者:saltcod

我刚刚将我所有的博客帖子都转换为CPT,因为我需要它们具有层次结构。问题是,我为所有的帖子都分配了类别。如何在我的CPT中使用标准WordPress类别?

我尝试添加\'taxonomies\' => array(\'category\'),register_post_type 功能,但不起作用。我试过了register_taxonomy_for_object_type( \'category\', \'illustrations\' ) 这也没用。

有什么建议吗?

更新:感谢所有人的回复,但如上所述,我两次都试过了\'taxonomies\' => array(\'category\')\'taxonomies\' => array(\'category\'), 两者都不起作用。

下面是整个register\\u post\\u type函数-带有“taxonomies”参数:

/* Register the \'Illustrations\' post type */

register_post_type(
    \'illustrations\',
    array(
        \'label\' => \'illustrations\',
        \'description\' => \'\',
        \'public\' => true,
        \'menu_icon\' => get_bloginfo( \'template_directory\' ) . \'/images/help-icon.png\',
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'capability_type\' => \'post\',
        \'taxonomies\' => array( \'category\' ),
        \'has_archive\' => true,
        \'hierarchical\' => true,
        \'rewrite\' => array( \'slug\' => \'\' ),
        \'query_var\' => true,
        \'supports\' => array( \'title\', \'editor\', \'custom-fields\', \'thumbnail\', \'page-attributes\', \'revisions\', \'comments\',\'author\' ),
        \'labels\' => array (
            \'name\' => \'Illustrations\',
            \'singular_name\' => \'Illustration\',
            \'menu_name\' => \'Illustrations\',
            \'add_new\' => \'Add Illustration\',
            \'add_new_item\' => \'Add new Illustration\',
            \'edit\' => \'Edit\',
            \'edit_item\' => \'Edit illustration\',
            \'new_item\' => \'New illustration\',
            \'view\' => \'View illustration\',
            \'view_item\' => \'View illustration\',
            \'search_items\' => \'Search illustrations\',
            \'not_found\' => \'No Illustrations Found\',
            \'not_found_in_trash\' => \'No Illustrations Found in Trash\',
            \'parent\' => \'Parent Illustration\',
            ), ) );

register_taxonomy_for_object_type( \'category\', \'illustrations\' );

4 个回复
最合适的回答,由SO网友:Eric Holmes 整理而成

答案在于register_taxonomy_for_object_type. 使用非常简单。

您还可以尝试在register_post_type.

SO网友:Milo

默认情况下,对类别页面的查询将仅查询post post类型,因此除了在taxonomies参数中使用category注册post类型外,还必须通过pre_get_posts 要使CPT显示在类别页面上,请执行以下操作:

function wpa_illustrations_on_category_pages( $query ) {
    if ( $query->is_category() && $query->is_main_query() ) {
        $query->set( \'post_type\', array( \'post\', \'illustrations\' ) );
    }
}
add_action( \'pre_get_posts\', \'wpa_illustrations_on_category_pages\' );

SO网友:Howdy_McGee

Maybe try this:

    /** Add Custom Taxonomy **/
    function illustrations_category_taxonomy() {
        $labels = array(
            \'name\'              => __( \'Categories\' ),
            \'singular_name\'     => __( \'Category\' ),
            \'search_items\'      => __( \'Search Categories\' ),
            \'all_items\'         => __( \'All Categories\' ),
            \'parent_item\'       => __( \'Parent Category\' ),
            \'parent_item_colon\' => __( \'Parent Category:\' ),
            \'edit_item\'         => __( \'Edit Category\' ), 
            \'update_item\'       => __( \'Update Category\' ),
            \'add_new_item\'      => __( \'Add New Category\' ),
            \'new_item_name\'     => __( \'New Category\' ),
            \'menu_name\'         => __( \'Categories\' ),
        );
        $args = array(
            \'labels\'            => $labels,
            \'hierarchical\'      => true
        );
        register_taxonomy( \'illustrations-category\', \'illustrations\', $args );
    }
    add_action( \'init\', \'illustrations_category_taxonomy\');
SO网友:Nicolai Grossherr

您需要使用taxonomies argument 像这样:

    \'taxonomies\' => array(\'category\')
在您的register_post_type 密码

其他信息:http://codex.wordpress.org/Function_Reference/register_post_type#Arguments

<小时>

Update:

/* Register the \'Illustrations\' post type */

    add_action( \'init\', \'wpse114838_reg_pt_illustrations\');
    function wpse114838_reg_pt_illustrations() { 
        register_post_type(
            \'illustrations\',
            array(
                \'labels\' => array (
                \'name\' => \'Illustrations\',
                \'singular_name\' => \'Illustration\',
                \'menu_name\' => \'Illustrations\',
                \'add_new\' => \'Add Illustration\',
                \'add_new_item\' => \'Add new Illustration\',
                \'edit\' => \'Edit\',
                \'edit_item\' => \'Edit illustration\',
                \'new_item\' => \'New illustration\',
                \'view\' => \'View illustration\',
                \'view_item\' => \'View illustration\',
                \'search_items\' => \'Search illustrations\',
                \'not_found\' => \'No Illustrations Found\',
                \'not_found_in_trash\' => \'No Illustrations Found in Trash\',
                \'parent\' => \'Parent Illustration\',
                ),
                \'label\' => \'illustrations\',
                \'description\' => \'\',
                \'public\' => true,
                \'menu_icon\' => get_bloginfo( \'template_directory\' ) . \'/images/help-icon.png\',
                \'show_ui\' => true,
                \'show_in_menu\' => true,
                \'capability_type\' => \'post\',
                //you need just one of the methods, uncomment the one you want
                //\'taxonomies\' => array( \'category\' ),
                \'has_archive\' => true,
                \'hierarchical\' => true,
                \'rewrite\' => array( \'slug\' => \'\' ),
                \'query_var\' => true,
                \'supports\' => array( \'title\', \'editor\', \'custom-fields\', \'thumbnail\', \'page-attributes\', \'revisions\', \'comments\',\'author\' ),
            )
        );
        //you need just one of the methods, uncomment the one you want    
        //register_taxonomy_for_object_type( \'category\', \'illustrations\' );
    }
这是基于您问题中的代码。我测试了它register_taxonomy_for_object_type( \'category\', \'illustrations\' );\'taxonomies\' => array( \'category\' ) - 方法,它正在发挥作用。只需使用其中一个,两个都做没有意义,请在上面的代码中取消注释所需的一个。你可能没有像“EricHolmes”和“Howdy\\u McGee”所建议的那样参与初始化操作。

结束

相关推荐

Extracting post categories

我正在帮助一位同事重新配置Wordpress数据库,以便在其他平台上使用。数据库基本上是关于所有帖子和附带元数据等的详细信息。我一直坚持的一件事是确定如何将每个帖子与其Wordpress类别关联起来。基本上,我希望能够将每个帖子及其对应的类别提取到电子表格中,要做到这一点,我显然需要知道它们在表格中的关系。有人能给我指出正确的方向吗?如有任何帮助,我们将不胜感激。非常感谢。