'supports' => array('title','author','post-formats'), 现在显示所有类型的后格式,但我想只显示选定的。
喜欢:链接,音频,视频
我想要这样的东西:

发布于 2018-04-03 06:28:48
你可以这样做:
add_theme_support( 'post-formats', array( 'link', 'audio', 'video' ) );默认情况下,它会添加所有已注册的格式,但就像这样,您可以选择添加哪种格式
您可以在代码库:Codex中阅读有关不同格式的内容,以及如何添加它们。
编辑:
如果您使用的是子主题,并且不想使用其他格式,则可以调用以下内容:
add_action( 'after_setup_theme', 'childtheme_formats', 11 );
function childtheme_formats(){
add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link' ) );
}编辑:
根据注释,您只希望在单个post类型上这样做:
然后你就可以这样做:
<?php add_post_type_support( $post_type, $supports ) ?>其中$support可以是字符串或数组:所以在您的任务中:
所以你也许可以做这样的事情:
function test_add_formats_support_for_cpt() {
add_post_type_support( 'yourCustomPostType', 'post-formats', array('link', 'audio', 'video') );
}
add_action( 'init', 'test_add_formats_support_for_cpt' );这是未经测试的,所以我不确定它是否有效-让我知道。
发布于 2018-04-03 07:40:12
您可以通过覆盖默认的post格式来限制或管理自定义post类型格式。
创建一个函数,返回我们的post类型支持的post格式数组,如音频、图库、图像和视频。
function customposttype_allowed_formats() {
return array( 'audio', 'gallery', 'image', 'video' );
}我们将使用“主题支持”系统并更改主题支持的格式,并将限制在我们的帖子类型仪表板屏幕上,这样它就不会与其他帖子类型混淆。
add_action( 'load-post.php', 'support_customposttype_filter' );
add_action( 'load-post-new.php', 'support_customposttype_filter' );
add_action( 'load-edit.php', 'support_customposttype_filter' );
function support_customposttype_filter() {
$screen = get_current_screen();
// Return if not customposttype screen.
if ( empty( $screen->post_type ) || $screen->post_type !== 'custom_post_type' )
return;
// Check 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], customposttype_allowed_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', 'customposttype_format_filter', 95 );
}在默认的post格式的末尾有一个过滤器,如果它不是被批准的格式之一(音频、画廊、图像和视频),我们可以覆盖默认的post格式。
function customposttype_format_filter( $format ) {
return in_array( $format, customposttype_allowed_formats() ) ? $format : 'standard';
}https://stackoverflow.com/questions/49623130
复制相似问题