我已经创建了一个与我的CPT关联的自定义分类法。两者都出现在我的仪表板上,问题是当我添加内容并想从自定义分类法列表中选择一个术语时,没有值(没有列表,没有复选框…)。我正在使用wordpress 5.1。下面是添加到函数的代码。php:
function type_custom_taxonomy() {
$labels = array(
\'name\' => _x( \'Types\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Type\', \'taxonomy singular name\' ),
\'menu_name\' => __( \'Types\' ),
);
register_taxonomy(\'types\',array(\'action\'), array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_rest\' => true,
\'show_tagcloud\' => false,
));
}
add_action( \'init\', \'type_custom_taxonomy\', 0 );
//CPT
function action_post_type() {
register_post_type( \'action\',
array(
\'labels\' => array(
\'name\' => __( \'Actions\' ),
\'singular_name\' => __( \'Action\' )
),
\'public\' => true,
\'has_archive\' => true,
\'show_in_rest\' => true,
\'supports\' => array(\'title\', \'editor\',\'thumbnail\'),
\'taxonomies\' => array(\'types\')
)
);
}
add_action( \'init\', \'action_post_type\' );
SO网友:Ali Gamaan
我面临的问题与你的问题相同,解决方法如下
您必须添加\'show_in_rest\' => true,
对于post\\u类型和分类,int数组的最后一行,例如
register_post_type(
\'portfolio\',
array(
\'labels\' => $labels,
\'exclude_from_search\' => false,
\'has_archive\' => true,
\'public\' => true,
\'publicly_queryable\' => false,
\'rewrite\' => false,
\'can_export\' => true,
\'show_in_nav_menus\' => true,
\'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'comments\', \'page-attributes\',\'excerpt\'),
\'show_in_rest\' =>true,
)
);
分类学
register_taxonomy(
\'portfoliocat\',
\'portfolio\',
array(
\'hierarchical\' => true,
\'show_in_nav_menus\' => true,
\'labels\' =>array(),
\'query_var\' => true,
\'rewrite\' => array(\'slug\' => \'portfoliocat\'),
\'show_in_rest\' => true,
)
);
SO网友:Krzysiek Dróżdż
我猜这是由优先顺序引起的。。。
您的两个操作使用相同的优先级。所以如果type_custom_taxonomy
函数被称为第一个函数,然后action
此时不存在帖子类型。
我会尝试这样的方式:
function action_post_type() {
register_post_type( \'action\',
array(
\'labels\' => array(
\'name\' => __( \'Actions\' ),
\'singular_name\' => __( \'Action\' )
),
\'public\' => true,
\'has_archive\' => true,
\'show_in_rest\' => true,
\'supports\' => array(\'title\', \'editor\',\'thumbnail\'),
)
);
$labels = array(
\'name\' => _x( \'Types\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Type\', \'taxonomy singular name\' ),
\'menu_name\' => __( \'Types\' ),
);
register_taxonomy(\'types\',array(\'action\'), array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_rest\' => true,
\'show_tagcloud\' => false,
));
}
add_action( \'init\', \'action_post_type\' );
这样,您可以确保在分类之前注册了CPT,并且可以将该分类分配给该CPT。