向您的CPT添加post格式支持后:
function jt_add_post_type_support() {
//add post-formats to post_type \'your_custom_post_type\'
add_post_type_support( \'your_custom_post_type\', \'post-formats\' );
}
add_action( \'init\', \'jt_add_post_type_support\', 10 );
您可以在以下情况下对其进行修改:(*不要忘记在两个函数中将“your\\u custom\\u post\\u type”更改为您的CPT)
add_action( \'load-post.php\', \'jt_post_format_support_filter\' );
add_action( \'load-post-new.php\', \'jt_post_format_support_filter\' );
add_action( \'load-edit.php\', \'jt_post_format_support_filter\' );
function jt_get_allowed_project_formats() {
//CHANGE TO THE ONES YOU WANT HERE:
return array( \'standard\', \'quote\', \'image\', \'video\' );
}
function jt_default_post_format_filter( $format ) {
return in_array( $format, jt_get_allowed_project_formats() ) ? $format : \'standard\';
}
function jt_post_format_support_filter() {
$screen = get_current_screen();
// Bail if not on the projects screen.
if ( empty( $screen->post_type ) || $screen->post_type !== \'your_custom_post_type\' )
return;
// Check if the current theme supports formats.
if ( current_theme_supports( \'post-formats\' ) ) {
$formats = get_theme_support( \'post-formats\' );
// If we have formats, add theme support for only the allowed formats.
if ( isset( $formats[0] ) ) {
$new_formats = array_intersect( $formats[0], jt_get_allowed_project_formats() );
// Remove post formats support.
remove_theme_support( \'post-formats\' );
// If the theme supports the allowed formats, add support for them.
if ( $new_formats )
add_theme_support( \'post-formats\', $new_formats );
}
}
// Filter the default post format.
add_filter( \'option_default_post_format\', \'jt_default_post_format_filter\', 95 );
}