自定义帖子类型不符合分配的类别

时间:2017-11-22 作者:Stefan

我创建了一个自定义的帖子类型“media”,并添加了几个分配了不同类别的帖子(新闻稿、媒体资源、新闻文章)

在我的媒体存档模板上,我使用WP\\u Query尝试显示每个模板中最近的3个。相反,每个部分都显示其中最新的部分。

此外,在“归档”页面上,我看到我的分类列表,但当我单击其中任何一个时,它们都会说“没有帖子”,因此我的CPT根本无法很好地处理这些分类,感谢您的帮助!

function create_post_type_media()
{
    register_taxonomy_for_object_type(\'category\', \'media\'); // Register Taxonomies for Category
    register_taxonomy_for_object_type(\'post_tag\', \'media\');
    register_post_type(\'media\', // Register Custom Post Type
        array(
        \'labels\' => array(
            \'name\' => __(\'Media Posts\', \'html5blank\'), // Rename these to suit
            \'singular_name\' => __(\'Media Post\', \'html5blank\'),
            \'add_new\' => __(\'Add New\', \'html5blank\'),
            \'add_new_item\' => __(\'Add New Media Post\', \'html5blank\'),
            \'edit\' => __(\'Edit\', \'html5blank\'),
            \'edit_item\' => __(\'Edit Media Post\', \'html5blank\'),
            \'new_item\' => __(\'New Media Post\', \'html5blank\'),
            \'view\' => __(\'View Media Post\', \'html5blank\'),
            \'view_item\' => __(\'View Media Post\', \'html5blank\'),
            \'search_items\' => __(\'Search Media Post\', \'html5blank\'),
            \'not_found\' => __(\'No Media Posts found\', \'html5blank\'),
            \'not_found_in_trash\' => __(\'No Media Posts found in Trash\', \'html5blank\')
        ),
        \'public\' => true,
        \'hierarchical\' => true, // Allows your posts to behave like Hierarchy Pages
        \'has_archive\' => true,
        \'supports\' => array(
            \'title\',
            \'editor\',
            \'excerpt\',
            \'thumbnail\'
        ), // Go to Dashboard Custom HTML5 Blank post for supports
        \'can_export\' => true, // Allows export in Tools > Export
        \'taxonomies\' => array(
            \'post_tag\',
            \'category\'
        )
    ));
}
add_action(\'init\', \'create_post_type_media\');
以下是我的一个WP\\U查询供参考:

$args = array(
    \'showposts\' => 3,
    \'post_type\' => \'media\',
    \'category_name\' => \'media-resource\',
);
$query = new WP_Query( $args );
if ($query->have_posts()) : ?>

1 个回复
SO网友:Agustin Prosperi

我要引用这个答案https://wordpress.stackexchange.com/a/242254/91219

Order really shouldn\'t really matter but it\'s good practice to register things in a hierarchical structure.

    register_post_type( $cpt, $args );
    register_taxonomy( $catname, $cpt );

结束