自定义邮政类型和自定义分类未连接

时间:2014-11-11 作者:NotaGuruAtAll

我使用以下代码创建自定义帖子类型和自定义分类法:

// Register Post Type
add_action( \'init\', \'cptui_register_my_cpt_resource\' );
function cptui_register_my_cpt_resource() {
    register_post_type( \'resource\', array(
        \'label\' => \'Resources\',
        \'description\' => \'\',
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'capability_type\' => \'post\',
        \'map_meta_cap\' => true,
        \'hierarchical\' => false,
        \'rewrite\' => array(\'slug\' => \'resource\', \'with_front\' => true),
        \'query_var\' => true,
        \'supports\' => array( \'title\', \'editor\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'thumbnail\', \'author\', \'page-attributes\',\' post-formats\' ),
        \'labels\' => array (
            \'name\' => \'Resources\',
            \'singular_name\' => \'Resource\',
            \'menu_name\' => \'Resources\',
            \'add_new\' => \'Add Resource\',
            \'add_new_item\' => \'Add New Resource\',
            \'edit\' => \'Edit\',
            \'edit_item\' => \'Edit Resource\',
            \'new_item\' => \'New Resource\',
            \'view\' => \'View Resource\',
            \'view_item\' => \'View Resource\',
            \'search_items\' => \'Search Resources\',
            \'not_found\' => \'No Resources Found\',
            \'not_found_in_trash\' => \'No Resources Found in Trash\',
            \'parent\' => \'Parent Resource\',
        )
    )); 
}

// Register Taxonomy 
add_action( \'init\', \'cptui_register_my_taxes_resource_categories\' );
function cptui_register_my_taxes_resource_categories() {
    register_taxonomy( \'resource categories\', array( 0 => \'resource\' ), array( 
        \'hierarchical\' => true,
        \'label\' => \'Resource Categories\',
        \'show_ui\' => true,
        \'query_var\' => true,
        \'show_admin_column\' => false,
        \'labels\' => array (
            \'search_items\' => \'Resource Category\',
            \'popular_items\' => \'\',
            \'all_items\' => \'\',
            \'parent_item\' => \'\',
            \'parent_item_colon\' => \'\',
            \'edit_item\' => \'\',
            \'update_item\' => \'\',
            \'add_new_item\' => \'\',
            \'new_item_name\' => \'\',
            \'separate_items_with_commas\' => \'\',
            \'add_or_remove_items\' => \'\',
            \'choose_from_most_used\' => \'\',
        )
    )); 
}
一切似乎都在表面上运行,我可以将一个类别连接到一个资源,但当我尝试这样查询它们时:

$args = array(
    \'suppress_filters\' => false,
    \'numberposts\' => -1,
    \'post_type\' => \'resource\',
    \'category_name\' => \'recruiting\'

);
$the_query = new WP_Query( $args );
我没有得到任何结果。有什么想法吗?

2 个回复
SO网友:ManuA

您的自定义分类名称是什么?

请尝试以下代码,替换taxonomy_name 不管你的实际分类名称是什么。

$args = array(
    \'suppress_filters\' => false,
    \'numberposts\' => -1,
    \'post_type\' => \'resource\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'taxonomy_name\',
            \'field\'    => \'slug\', //Select taxonomy term by slug( you can also use name/id)
            \'terms\'    => \'term-slug/category -slug\',
        ),

);

$the_query = new WP_Query( $args );

Custom taxonomy parameter

SO网友:NotaGuruAtAll

啊,我没有使用自定义分类法名称,即“资源类别”,显然这些类别可以包含空格,因为这样做有效:

$args = array(
    \'suppress_filters\' => false,
    \'numberposts\' => 3,
    \'post_type\' => \'resource\',
    \'resource categories\' => \'recruiting\'
);

结束

相关推荐