我已经设置了一个按预期工作的自定义帖子类型:
add_action( \'init\', __NAMESPACE__ . \'\\\\work_post_type\' );
function work_post_type() {
// set up labels
$labels = array(
\'name\' => \'Work\',
\'singular_name\' => \'Work\',
\'add_new\' => \'Add New Work\',
\'add_new_item\' => \'Add New Work\',
\'edit_item\' => \'Edit Work\',
\'new_item\' => \'New Work\',
\'all_items\' => \'All Work\',
\'view_item\' => \'View Work\',
\'search_items\' => \'Search Work\',
\'not_found\' => \'No Work Found\',
\'not_found_in_trash\' => \'No Work found in Trash\',
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Work\',
);
//register post type
register_post_type( \'work\', array(
\'labels\' => $labels,
\'has_archive\' => true,
\'public\' => true,
\'supports\' => array( \'title\', \'editor\', \'excerpt\', \'thumbnail\'),
\'taxonomies\' => array( \'client\', \'category\' ),
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'show_in_nav_menus\' => true,
\'show_ui\' => true,
\'capability_type\' => \'post\',
\'rewrite\' => array( \'slug\' => \'work\' ),
)
);
}
如你所见,我已经包括
category
作为此自定义帖子类型的分类。我添加了两个在网站上使用的类别。但是,如果我转到我添加的任何类别的存档,它不会返回任何帖子,尽管有相关的帖子。是因为它是一种自定义的帖子类型,而查询无法识别它吗?
我觉得奇怪的是我自定义的分类法
client
效果很好。
最合适的回答,由SO网友:Dave Romsey 整理而成
WordPress仅包括post
默认情况下,类别存档的帖子类型,但您可以通过更改查询来添加其他帖子类型:
function wpse241719_add_custom_types_to_category_archives( $query ) {
if ( ! is_admin() && is_category() && $query->is_main_query() ) {
$query->set( \'post_type\', array(
\'post\',
\'work\',
// \'another_post_type\',
) );
}
}
add_filter( \'pre_get_posts\', \'wpse241719_add_custom_types_to_category_archives\' );