Thanks guys, I found a solution:
It’s as easy as adding the post_type_category
element to the arguments object within register_post_type():
$args = array(
'label' => 'sausages',
'description' => 'Sausages',
'labels' => $labels,
'post_type_category' => 'food',
'supports' => array( 'title', 'category' ),
'hierarchical' => false,
/* and so on */
);
register_post_type( 'sausage', $args );
And then add this to your functions.php:
function is_post_type_in_cat ( $category = null , $post_type = null ) {
if(!$post_type) $post_type = get_post_type();
$post_type_object = get_post_type_object( $post_type );
$arr = isset($post_type_object->post_type_category) ? $post_type_object->post_type_category : null;
if( !$arr ) return false;
if ( !is_array($arr) )
$arr = array($arr);
return in_array($category, $arr);
}
You can then check in your theme with the following code:
if( is_post_type_in_cat( 'food' ) ) :
//do something
else:
//do something
endif;
You can also not only for the current but for a specific post type:
if( is_post_type_in_cat( 'food', 'sausage' ) ) :
//do something
else:
//do something
endif;
Also an array of categories can be given as a parameter:
if( is_post_type_in_cat( array('food', 'animal', 'dairy', 'cheese'), 'gouda' ) ) : // and so on
Easier than I thought. Thanks to all contributors!
Perhaps in the future I’m gonna build a lightweight plugin with a GUI for this if this will not be implemented in the major CPT-Plugins or even the WP core.