微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CMB2 灵活内容字段控件不起作用帮我创建一个可重复的组并添加拖放控件

如何解决CMB2 灵活内容字段控件不起作用帮我创建一个可重复的组并添加拖放控件

我的任务是使用 CMB2 管理自定义字段来构建一个站点

当然,我试图对其进行过度设计,因此这是一个简化的过程,因此一直在尝试利用此资源:https://github.com/reaktivstudios/cmb2-flexible-content

我已经安装了 CMB2 wordpress 插件

我已经提取上传并启用了上面指定的 CMB2 扩展插件

我现在正在尝试将其模块化构建到我的主题中,以便可以无缝地将其拖放到另一个网站。文件夹的结构如下:

Folder Structure

我现在将向您展示文件内容,以及它们如何连接。

FC-FUNCTIONS.PHP - 这是创建一个可以在页面模板上调用以启动内容创建的函数

function flexible_content() {
    require_once(get_stylesheet_directory().'/modules/flexible-content/flexible-content.PHP');
}

LAYOUTS.PHP - 此文件将包含所有字段组,按布局分组:

add_action( 'cmb2_admin_init','fc_register_flexible_content_cmb2_field' );
function fc_register_flexible_content_cmb2_field() {

    $prefix = '_fc_';

    // Basic CMB2 MetaBox declaration
    $cmb = new_cmb2_Box(
        array(
            'id'            => $prefix . 'flexible_content_types','title'         => __( 'Flexible Content Types' ),'object_types'  => array( 'page',),)
    );

    // Then add your flexible field deFinition. Each layout group should be defined in the layouts array,with the `ID` for that group as its key. Each layout group can contain a `title` and a list of CMB2 `fields`.

    $cmb->add_field(
        array(
            'name'          => __( 'Flexible Content','flexible_content' ),'desc'          => __( 'Add flexible content layouts','id'            => $prefix . 'flexible_content','type'          => 'flexible','priority'      => 'high','show_names'    => true,'options'       => array(
                'flexible_title'=> __( 'Row {#}','add_button'    => __( 'Add Row','remove_button' => __( 'Remove Row','sortable'      => true,'layouts'       => array(
                'hero_banner'   => array(
                    'title'         => 'Hero Banners','type'          => 'group','description'   => __( 'Add slides into the hero banner','hero_banners' ),'repeatable'    => true,'options'       => array(
                        'group_title'   => __( 'Banners {#}','add_button'    => __( 'Add Another Banner','remove_button' => __( 'Remove Banner','fields'        => array(
                        array(
                            'type'      => 'text','name'      => 'heading','desc'      => 'Hero Slide Title','id'        => 'heading'
                        ),array(
                            'type'      => 'textarea','name'      => 'Subheading','desc'      => 'Hero Slide Subheading Textarea','id'        => 'subheading'
                        ),array(
                            'type'      => 'text','name'      => 'Hero Slide Link Text','id'        => 'link_text'
                        ),array(
                            'type'      => 'text_url','name'      => 'Hero Slide Link URL','id'        => 'link_url'
                        )
                    )
                ),'fc_free_text'  => array(
                    'title'         => 'Text Group','name'      => 'Title text','id'        => $prefix . 'title','name'      => 'Description textarea','id'        => $prefix . 'description',)
                    )
                ),)
        )
    );
}

FLEXIBLE-CONTENT.PHP - 这决定了为在 wordpress 页面后端填充的 layouts.PHP 中声明的每个选定和填充的字段抓取哪个布局。

$prefix = '_fc_';
$flexible_fields = get_post_meta( get_the_ID(),$prefix . 'flexible_content',true );

if ($flexible_fields != '') {

    echo '<div class="fc-content">';

    foreach( $flexible_fields as $field ) {

        if ( 'fc_free_text' === $field['layout'] ) {

            // Free Text
            require('layouts/inc/templates/fc-free-text.PHP');

        }

    }

    echo '</div><!-- fc-content -->';

}

我面临的主要问题是按钮不会显示给我,向上/向下箭头和中继器组“添加一个横幅”按钮。但是,我可以查看字段、添加/删除布局、填充内容并在前端输出

请帮助我使 Hero Banner 组字段可重复并且每个布局都可拖放。

这是正在处理的其中一个页面的后端图像,您可以看到它们不可排序,并且没有选项(在创建中指定)重复和创建多个英雄横幅相同的分组:

Flexible Content Page Backend

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。