每种定制帖子类型的帖子格式选项是否不同?

时间:2011-05-01 作者:Mike

我想为每种帖子类型添加帖子格式。例如,我有一个帖子类型“Gallery”。我想在这篇文章中添加“图片”、“图库”和“视频”。在普通帖子中,我想使用另一个帖子格式列表。

我尝试:

function postf()
{
    global $post_ID;

    $postType = get_post_type( $post_ID );

    if( $_GET[\'post_type\'] || $postType == \'gallery-custompost\' )
    {
        add_theme_support( \'post-formats\', array( \'image\', \'gallery\', \'video\' ) );
        add_post_type_support( \'gallery-custompost\', \'post-formats\' );
    }
}
add_action(\'init\', \'postf\');
当我添加一篇新文章时,它会起作用,但当我尝试编辑它时,文章格式不会出现。

有人知道我该怎么做吗?

3 个回复
SO网友:Javier Villanueva

注册帖子类型时,请尝试在参数上添加帖子格式支持,而不是使用add_post_type_support, 例如:

$args = array(
    ...
    \'supports\' => array(\'title\', \'editor\', \'author\', \'post-formats\')
); 
register_post_type(\'gallery-custompost\', $args);
看看运气如何。

SO网友:Jan Fabry

您可以更改“帖子格式”元框中显示的列表,但在保存帖子时,将对照注册的帖子格式检查提交的值,并且您的代码可能没有在那里运行。

我的解决方案是注册您将用于主题的所有帖子格式,然后根据帖子类型对其进行限制,将标准元框替换为您自己的元框。这只会限制元框中的可用选项,在保存帖子时不会限制这些选项,但您可能可以添加额外的检查,以防止冒险用户设置“不允许的”帖子格式。

// Register all post types that we will use
add_action( \'after_setup_theme\', \'wpse16136_after_setup_theme\', 11 );
function wpse16136_after_setup_theme()
{
    add_theme_support( \'post-formats\', array( \'aside\', \'gallery\', \'video\' ) );
}

// Register our custom post type, and link the post formats to the post types
// Yes, you can (ab)use add_post_type_support to save extra data like this
add_action( \'init\', \'wpse16136_init\' );
function wpse16136_init()
{
    register_post_type( \'wpse16136\', array(
        \'public\' => true,
        \'label\' => \'WPSE 16136\',
        \'supports\' => array( \'title\', \'editor\' ),
    ) );
    add_post_type_support( \'wpse16136\', \'post-formats\', array( \'gallery\', \'video\' ) );
    add_post_type_support( \'post\', \'post-formats\', array( \'aside\', \'gallery\' ) );
}

// Replace the standard meta box callback with our own
add_action( \'add_meta_boxes\', \'wpse16136_add_meta_boxes\' );
function wpse16136_add_meta_boxes( $post_type )
{
    if ( ! get_post_type_object( $post_type ) ) {
        // It\'s a comment or a link, or something else
        return;
    }
    remove_meta_box( \'formatdiv\', $post_type, \'side\' );
    add_meta_box( \'wpse16136_formatdiv\', _x( \'Format\', \'post format\' ), \'wpse16136_post_format_meta_box\', $post_type, \'side\', \'core\' );
}

// This is almost a duplicate of the original meta box
function wpse16136_post_format_meta_box( $post, $box ) {
    if ( current_theme_supports( \'post-formats\' ) && post_type_supports( $post->post_type, \'post-formats\' ) ) :
    $post_formats = get_theme_support( \'post-formats\' );

    // This is our extra code
    // If the post type has registered post formats, use those instead
    if ( is_array( $GLOBALS[\'_wp_post_type_features\'][$post->post_type][\'post-formats\'] ) ) {
        $post_formats = $GLOBALS[\'_wp_post_type_features\'][$post->post_type][\'post-formats\'];
    }

    if ( is_array( $post_formats[0] ) ) :
        $post_format = get_post_format( $post->ID );
        if ( !$post_format )
            $post_format = \'0\';
        $post_format_display = get_post_format_string( $post_format );
        // Add in the current one if it isn\'t there yet, in case the current theme doesn\'t support it
        if ( $post_format && !in_array( $post_format, $post_formats[0] ) )
            $post_formats[0][] = $post_format;
    ?>
    <div id="post-formats-select">
        <input type="radio" name="post_format" class="post-format" id="post-format-0" value="0" <?php checked( $post_format, \'0\' ); ?> /> <label for="post-format-0"><?php _e(\'Standard\'); ?></label>
        <?php foreach ( $post_formats[0] as $format ) : ?>
        <br /><input type="radio" name="post_format" class="post-format" id="post-format-<?php echo esc_attr( $format ); ?>" value="<?php echo esc_attr( $format ); ?>" <?php checked( $post_format, $format ); ?> /> <label for="post-format-<?php echo esc_attr( $format ); ?>"><?php echo esc_html( get_post_format_string( $format ) ); ?></label>
        <?php endforeach; ?><br />
    </div>
    <?php endif; endif;
}

SO网友:Chip Bennett

Post格式是一种自定义分类法,为“Post”Post类型注册。如果您想将“Post Format”分类法用于不同的Post类型,那么您需要为该Post类型注册它,而不仅仅是为“Post”Post类型启用对它的支持。

结束

相关推荐