首页 > 解决方案 > 我有一组 CMB2 字段,我正在将 url 元导入到,它适用于单个字段,但不适用于分组

问题描述

我有一个需要添加 url 元数据的字段,到目前为止,我有这个允许我将元数据添加到单个字段。

add_action( 'cvm_post_insert', 'add_extra_meta_data', 40, 3 );

函数 add_extra_meta_data( $post_id, $video, $theme_import ){

if ( get_post_status( $post_id ) == 'draft' ) {
    $url = cvm_video_url($video['video_id']);
    if($url){
        update_post_meta($post_id, 'exercise_gallery_embed', $url);
    }
}

}

但是当我使用分组字段时,输入字段名称看起来更像这样,因为它正在堆叠 id

add_action( 'cvm_post_insert', 'add_extra_meta_data', 40, 3 );

函数 add_extra_meta_data( $post_id, $video, $theme_import ){

if ( get_post_status( $post_id ) == 'draft' ) {
    $url = cvm_video_url($video['video_id']);
    if($url){
        update_post_meta($post_id, 'exercise_gallery[0][exercise_gallery_embed]', $url);
    }
}

}

虽然我可以在自定义字段中看到数据,但它不会将 url 添加到元框,欢迎任何帮助或指针。以下是正在使用的字段组。

    // Repeatable group.
    $gallery_group = $cmb->add_field(
        array(
            'id'      => $this->slug . '_gallery',
            'type'    => 'group',
            'options' => array(
                'group_title'   => __( 'Gallery', 'lsx-health-plan' ) . ' {#}', // {#} gets replaced by row number
                'add_button'    => __( 'Add Item', 'lsx-health-plan' ),
                'remove_button' => __( 'Remove Item', 'lsx-health-plan' ),
                'sortable'      => true,
            ),
            'desc'    => __( 'Upload only one image, video or gif per gallery group, each group will only display 1 item.', 'lsx-health-plan' ),
            'classes' => 'lsx-admin-row',
        )
    );

    // Title.
    $cmb->add_group_field(
        $gallery_group,
        array(
            'name'       => __( 'Image', 'lsx-health-plan' ),
            'id'         => $this->slug . '_gallery_image',
            'type'       => 'file',
            'text'       => array(
                'add_upload_file_text' => __( 'Add File', 'lsx-health-plan' ),
            ),
            'desc'       => __( 'Upload an image a minimum of 800px x 600px in size.', 'lsx-health-plan' ),
            'query_args' => array(
                'type' => array(
                    'image/gif',
                    'image/jpeg',
                    'image/png',
                ),
            ),
            'preview_size' => 'lsx-thumbnail-wide',
            'classes' => 'lsx-field-col lsx-field-col-80',
        )
    );

    // Title.
    $cmb->add_group_field(
        $gallery_group,
        array(
            'name'    => __( 'oEmbed', 'lsx-health-plan' ),
            'id'      => $this->slug . '_gallery_embed',
            'type'    => 'text',
            'desc'    => __( 'Drop in the embed url for your video from YouTube, Vimeo or DailyMotion, e.g: "https://www.youtube.com/watch?v=9xwazD5SyVg". A full list of supports formats can be found at <a href="https://make.wordpress.org/support/user-manual/content/media/adding-media-to-your-pages-and-posts/embedding-media-from-other-sites/">WordPress</a>', 'lsx-health-plan' ),
            'classes' => 'test-apply-form lsx-field-col lsx-field-col-50',
        )
    );

标签: wordpressmeta-tagscmb2

解决方案


推荐阅读